11---
22description : Describes how to use operators to assign values to variables.
33Locale : en-US
4- ms.date : 08/ 01/2023
4+ ms.date : 01/19/2024
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_assignment_operators?view=powershell-5.1&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about Assignment Operators
@@ -15,8 +15,8 @@ Describes how to use operators to assign values to variables.
1515
1616Assignment operators assign one or more values to a variable. The equals sign
1717(` = ` ) is the PowerShell assignment operator. PowerShell also has the following
18- _ compound_ assignment operators: ` += ` , ` -= ` , ` *= ` , ` %= ` , ` ++ ` , ` -- ` . Compound
19- assignment operators perform numeric operations on the values before the
18+ _ compound_ assignment operators: ` += ` , ` -= ` , ` *= ` , ` %= ` , ` ++ ` , ` -- ` , ` ??= ` .
19+ Compound assignment operators perform operations on the values before the
2020assignment.
2121
2222## Syntax
321321```
322322
323323You can't use the ` -= ` operator to delete the values of a variable. To delete
324- all the values that are assigned to a variable, use the
325- [ Clear-Item] ( xref:Microsoft.PowerShell.Management.Clear-Item ) or
326- [ Clear-Variable] ( xref:Microsoft.PowerShell.Utility.Clear-Variable ) cmdlets
327- to assign a value of ` $null ` or ` "" ` to the variable.
324+ all the values that are assigned to a variable, use the [ Clear-Item] [ 06 ] or
325+ [ Clear-Variable] [ 07 ] cmdlets to assign a value of ` $null ` or ` "" ` to the
326+ variable.
328327
329328``` powershell
330329$a = $null
3553543
356355```
357356
358- To delete a variable, use the
359- [ Remove-Variable] ( xref:Microsoft.PowerShell.Utility.Remove-Variable )
360- cmdlet. This method is useful when the variable is explicitly cast to a
361- particular data type, and you want an untyped variable. The following command
362- deletes the ` $a ` variable:
357+ To delete a variable, use the [ Remove-Variable] [ 08 ] cmdlet. This method is
358+ useful when the variable is explicitly cast to a particular data type, and you
359+ want an untyped variable. The following command deletes the ` $a ` variable:
363360
364361``` powershell
365362Remove-Variable -Name a
@@ -416,8 +413,8 @@ $a[0] *= 2
416413### The assignment by division operator
417414
418415The assignment by division operator ` /= ` divides a numeric value by the value
419- that's specified on the right side of the operator. The operator can't be
420- used with string variables.
416+ that's specified on the right side of the operator. The operator can't be used
417+ with string variables.
421418
422419The ` /= ` operator combines two operations. First, it divides, and then it
423420assigns. Therefore, the following two statements are equivalent:
6816789
682679```
683680
684- You can cast a new scalar variable as any .NET type by placing the type name in
685- brackets that precede either the variable name or the first assignment value.
686- When you cast a variable, you are defining the type of data that can be stored
687- in the variable.
681+ You can cast a new [ scalar] [ 01 ] variable as any .NET type by placing the type
682+ name in brackets that precede either the variable name or the first assignment
683+ value. When you cast a variable, you are defining the type of data that can be
684+ stored in the variable.
688685
689686For example, the following command casts the variable as a string type:
690687
@@ -811,9 +808,9 @@ $d, $e, $f = $c
811808This command assigns the value 3 to the ` $d ` variable, the value 4 to the ` $e `
812809variable, and the value 5 to the ` $f ` variable.
813810
814- If the assignment value contains fewer elements than variables, the
815- remaining variables are assigned the value ` $null ` . For example, the
816- following command contains three variables and two values:
811+ If the assignment value contains fewer elements than variables, the remaining
812+ variables are assigned the value ` $null ` . For example, the following command
813+ contains three variables and two values:
817814
818815``` powershell
819816$a, $b, $c = 1, 2
@@ -832,21 +829,29 @@ $a = $b = $c = $d = "three"
832829
833830## Variable-related cmdlets
834831
835- In addition to using an assignment operation to set a variable value, you
836- can also use the
837- [ Set-Variable] ( xref:Microsoft.PowerShell.Utility.Set-Variable ) cmdlet. For
838- example, the following command uses ` Set-Variable ` to assign an array of 1,
839- 2, 3 to the ` $a ` variable.
832+ In addition to using an assignment operation to set a variable value, you can
833+ also use the [ Set-Variable] [ 09 ] cmdlet. For example, the following command uses
834+ ` Set-Variable ` to assign an array of 1, 2, 3 to the ` $a ` variable.
840835
841836``` powershell
842837Set-Variable -Name a -Value 1, 2, 3
843838```
844839
845840## See also
846841
847- - [ about_Arrays] ( about_Arrays.md )
848- - [ about_Hash_Tables] ( about_Hash_Tables.md )
849- - [ about_Variables] ( about_Variables.md )
850- - [ Clear-Variable] ( xref:Microsoft.PowerShell.Utility.Clear-Variable )
851- - [ Remove-Variable] ( xref:Microsoft.PowerShell.Utility.Remove-Variable )
852- - [ Set-Variable] ( xref:Microsoft.PowerShell.Utility.Set-Variable )
842+ - [ about_Arrays] [ 02 ]
843+ - [ about_Hash_Tables] [ 03 ]
844+ - [ about_Variables] [ 05 ]
845+ - [ Clear-Variable] [ 07 ]
846+ - [ Remove-Variable] [ 08 ]
847+ - [ Set-Variable] [ 09 ]
848+
849+ <!-- link references -->
850+ [ 01 ] : /powershell/scripting/learn/glossary#scalar-value
851+ [ 02 ] : about_Arrays.md
852+ [ 03 ] : about_Hash_Tables.md
853+ [ 05 ] : about_Variables.md
854+ [ 06 ] : xref:Microsoft.PowerShell.Management.Clear-Item
855+ [ 07 ] : xref:Microsoft.PowerShell.Utility.Clear-Variable
856+ [ 08 ] : xref:Microsoft.PowerShell.Utility.Remove-Variable
857+ [ 09 ] : xref:Microsoft.PowerShell.Utility.Set-Variable
0 commit comments