# about_Assignment_Operators

## 語法

• `<assignable-expression>` `<assignment-operator>` `<value>`

• `<assignable-expression><operator>`
• `<operator><assignable-expression>`

## 使用指派運算符

``````\$MyShell = "PowerShell"
``````

``````\$a = 6
\$a = 12
``````

``````\$a = Get-Process
\$a += Get-Service
\$a += "string"
\$a += 12
``````

``````\$a = Get-Service | Sort-Object -Property name
``````

``````\$a = if (\$b -lt 0) { 0 } else { \$b }
``````

``````\$a = "apple", "orange", "lemon", "grape"
``````

``````\$a = @{one=1; two=2; three=3}
``````

``````\$a = 0x10
``````

``````\$a = 3.1415e3
``````

PowerShell 也可以將 KB `KB`、MB `MB`和 GB 轉換成位元組 `GB` 。 例如，若要將 10 KB 的值指派給 `\$a` 變數，請輸入：

``````\$a = 10kb
``````

## 使用複合指派運算符

``````\$x = 1 # Global scope
& { \$x += 1; \$x }
``````
``````1
``````

``````\$x = 1 # Global scope
& { \$x = \$x + 1; \$x }
"Global `\$x = \$x"
``````
``````2
Global \$x = 1
``````

### 加法運算子指派

``````\$a += 2
\$a = (\$a + 2)
``````

``````\$a = 4
\$a += 2
\$a
``````
``````6
``````

``````\$a = "Windows"
\$a += " PowerShell"
\$a
``````
``````Windows PowerShell
``````

``````\$a = 1,2,3
\$a += 2
\$a
``````
``````1
2
3
2
``````

``````\$a += "String"
\$a
``````
``````1
2
3
2
String
``````

``````\$a = @{a = 1; b = 2; c = 3}
\$a += @{mode = "write"}
\$a
``````
``````Name                           Value
----                           -----
a                              1
b                              2
mode                           write
c                              3
``````

``````\$a = @{a = 1; b = 2; c = 3}
\$a += 1
``````
``````InvalidOperation:
Line |
2 |  \$a += 1
|  ~~~~~~~
| A hash table can only be added to another hash table.
``````

### 減法運算子指派

``````\$a -= 2
\$a = (\$a - 2)
``````

``````\$a = 8
\$a -= 2
\$a
``````
``````6
``````

``````\$a = 1,2,3
\$a[2] -= 1
\$a
``````
``````1
2
2
``````

``````\$a = \$null
``````

``````\$a = 1,2,3
\$a
``````
``````1
2
3
``````
``````\$a[1] = \$null
\$a
``````
``````1
3
``````

``````Remove-Variable -Name a
``````

### 乘法運算子指派

``````\$a = 3
\$a *= 4
\$a
``````
``````12
``````

``````\$a *= 2
\$a = (\$a * 2)
``````

``````\$a = "file"
\$a *= 4
\$a
``````
``````filefilefilefile
``````

``````\$a[0] *= 2
``````

### 依除法運算符指派

``````\$a /= 2
\$a = (\$a / 2)
``````

``````\$a = 8
\$a /=2
\$a
``````
``````4
``````

``````\$a[1] /= 2
``````

### 模數運算子指派

``````\$a %= 2
\$a = (\$a % 2)
``````

``````\$a = 7
\$a %= 4
\$a
``````
``````3
``````

### 遞增和遞減運算符

``````\$a = 7
++\$a
\$a
``````
``````8
``````

``````\$a = 7
(++\$a)
``````
``````8
``````

``````\$a = 7
\$c = ++\$a
\$a
``````
``````8
``````
``````\$c
``````
``````8
``````

``````\$a = 7
\$c = \$a++
\$a
``````
``````8
``````
``````\$c
``````
``````7
``````

``````\$a = 7
--\$a
\$a
``````
``````6
``````
``````(--\$a)
``````
``````5
``````

``````\$a = 7
\$c = --\$a
\$a
``````
``````6
``````
``````\$c
``````
``````6
``````

``````\$a = 7
\$d = \$a--
\$a
``````
``````6
``````
``````\$d
``````
``````7
``````

### Null 聯合指派運算符

Null 聯合指派運算符 `??=` 只有在左側操作數評估為 null 時，才會將右操作數的值指派給其左側操作數。 如果左方運算元評估為非 Null，`??=` 運算子不會評估其右方運算元。

``````\$x = \$null
\$x ??= 100
\$x
``````
``````100
``````

## Microsoft .NET 類型

``````\$a = 6
``````

``````\$a = 6
\$a.GetType().FullName
``````
``````System.Int32
``````

``````\$a = "6"
\$a.GetType().FullName
``````
``````System.String
``````

``````\$a = "file"
\$a += 3
\$a
``````
``````file3
``````

``````\$a = 6
\$a += "3"
\$a
``````
``````9
``````

``````[string]\$a = 27
\$a += 3
\$a
``````
``````273
``````

``````\$a = [string]27
``````

``````\$a = "string"
[int]\$a
``````
``````InvalidArgument: Cannot convert value "string" to type "System.Int32". Error:
"The input string 'string' was not in a correct format."
``````

``````[int]\$a = 3
``````

``````\$a = 3
\$a = "string"
[int]\$a = 3
\$a = "string"
``````
``````MetadataError:
Line |
2 |  \$a = "string"
|  ~~~~~~~~~~~~~
| Cannot convert value "string" to type "System.Int32". Error: "The input
string 'string' was not in a correct format."
``````
``````[string]\$a = "string"
``````

``````[string []] \$a = "one", "two", "three"
``````

PowerShell 變數可以是任何 .NET 數據類型。 此外，您可以指派目前程式中可用的任何完整 .NET 數據類型。 例如，下列命令會 `System.DateTime` 指定資料類型：

``````[System.DateTime]\$a = "5/31/2005"
``````

``````Tuesday, May 31, 2005 12:00:00 AM
``````

## 指派多個變數

``````\$a, \$b, \$c = 1, 2, 3
``````

``````\$a, \$b, \$c = 1, 2, 3, 4, 5
``````

``````\$d, \$e, \$f = \$c
``````

``````\$a, \$b, \$c = 1, 2
``````

``````\$a = \$b = \$c = \$d = "three"
``````

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