Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1/19/2024 PM Publish #10814

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Describes how to use operators to assign values to variables.
Locale: en-US
ms.date: 08/01/2023
ms.date: 01/19/2024
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_assignment_operators?view=powershell-5.1&WT.mc_id=ps-gethelp
schema: 2.0.0
title: about Assignment Operators
Expand All @@ -15,8 +15,8 @@ Describes how to use operators to assign values to variables.

Assignment operators assign one or more values to a variable. The equals sign
(`=`) is the PowerShell assignment operator. PowerShell also has the following
_compound_ assignment operators: `+=`, `-=`, `*=`, `%=`, `++`, `--`. Compound
assignment operators perform numeric operations on the values before the
_compound_ assignment operators: `+=`, `-=`, `*=`, `%=`, `++`, `--`, `??=`.
Compound assignment operators perform operations on the values before the
assignment.

## Syntax
Expand Down Expand Up @@ -321,10 +321,9 @@ $a
```

You can't use the `-=` operator to delete the values of a variable. To delete
all the values that are assigned to a variable, use the
[Clear-Item](xref:Microsoft.PowerShell.Management.Clear-Item) or
[Clear-Variable](xref:Microsoft.PowerShell.Utility.Clear-Variable) cmdlets
to assign a value of `$null` or `""` to the variable.
all the values that are assigned to a variable, use the [Clear-Item][06] or
[Clear-Variable][07] cmdlets to assign a value of `$null` or `""` to the
variable.

```powershell
$a = $null
Expand Down Expand Up @@ -355,11 +354,9 @@ $a
3
```

To delete a variable, use the
[Remove-Variable](xref:Microsoft.PowerShell.Utility.Remove-Variable)
cmdlet. This method is useful when the variable is explicitly cast to a
particular data type, and you want an untyped variable. The following command
deletes the `$a` variable:
To delete a variable, use the [Remove-Variable][08] cmdlet. This method is
useful when the variable is explicitly cast to a particular data type, and you
want an untyped variable. The following command deletes the `$a` variable:

```powershell
Remove-Variable -Name a
Expand Down Expand Up @@ -416,8 +413,8 @@ $a[0] *= 2
### The assignment by division operator

The assignment by division operator `/=` divides a numeric value by the value
that's specified on the right side of the operator. The operator can't be
used with string variables.
that's specified on the right side of the operator. The operator can't be used
with string variables.

The `/=` operator combines two operations. First, it divides, and then it
assigns. Therefore, the following two statements are equivalent:
Expand Down Expand Up @@ -681,10 +678,10 @@ $a
9
```

You can cast a new scalar variable as any .NET type by placing the type name in
brackets that precede either the variable name or the first assignment value.
When you cast a variable, you are defining the type of data that can be stored
in the variable.
You can cast a new [scalar][01] variable as any .NET type by placing the type
name in brackets that precede either the variable name or the first assignment
value. When you cast a variable, you are defining the type of data that can be
stored in the variable.

For example, the following command casts the variable as a string type:

Expand Down Expand Up @@ -811,9 +808,9 @@ $d, $e, $f = $c
This command assigns the value 3 to the `$d` variable, the value 4 to the `$e`
variable, and the value 5 to the `$f` variable.

If the assignment value contains fewer elements than variables, the
remaining variables are assigned the value `$null`. For example, the
following command contains three variables and two values:
If the assignment value contains fewer elements than variables, the remaining
variables are assigned the value `$null`. For example, the following command
contains three variables and two values:

```powershell
$a, $b, $c = 1, 2
Expand All @@ -832,21 +829,29 @@ $a = $b = $c = $d = "three"

## Variable-related cmdlets

In addition to using an assignment operation to set a variable value, you
can also use the
[Set-Variable](xref:Microsoft.PowerShell.Utility.Set-Variable) cmdlet. For
example, the following command uses `Set-Variable` to assign an array of 1,
2, 3 to the `$a` variable.
In addition to using an assignment operation to set a variable value, you can
also use the [Set-Variable][09] cmdlet. For example, the following command uses
`Set-Variable` to assign an array of 1, 2, 3 to the `$a` variable.

```powershell
Set-Variable -Name a -Value 1, 2, 3
```

## See also

- [about_Arrays](about_Arrays.md)
- [about_Hash_Tables](about_Hash_Tables.md)
- [about_Variables](about_Variables.md)
- [Clear-Variable](xref:Microsoft.PowerShell.Utility.Clear-Variable)
- [Remove-Variable](xref:Microsoft.PowerShell.Utility.Remove-Variable)
- [Set-Variable](xref:Microsoft.PowerShell.Utility.Set-Variable)
- [about_Arrays][02]
- [about_Hash_Tables][03]
- [about_Variables][05]
- [Clear-Variable][07]
- [Remove-Variable][08]
- [Set-Variable][09]

<!-- link references -->
[01]: /powershell/scripting/learn/glossary#scalar-value
[02]: about_Arrays.md
[03]: about_Hash_Tables.md
[05]: about_Variables.md
[06]: xref:Microsoft.PowerShell.Management.Clear-Item
[07]: xref:Microsoft.PowerShell.Utility.Clear-Variable
[08]: xref:Microsoft.PowerShell.Utility.Remove-Variable
[09]: xref:Microsoft.PowerShell.Utility.Set-Variable
Loading