編輯附註
重要
Windows PowerShell 語言規格 3.0 於 2012 年 12 月發行,並以 Windows PowerShell 3.0 為基礎。 此規格不會反映 PowerShell 的目前狀態。 沒有計劃更新此檔以反映目前的狀態。 此文件在此處提供作為歷史參考。
規格檔可從 Microsoft 下載中心取得 Microsoft Word 檔:https://www.microsoft.com/download/details.aspx?id=36389。該 Word 檔已在 Microsoft Learn 上轉換以供展示。 在轉換期間,已進行一些編輯變更,以配合 Docs 平臺的格式設定。 已修正某些錯字和次要錯誤。
語法:
expression:
primary-expression
bitwise-expression
logical-expression
comparison-expression
additive-expression
multiplicative-expression
dash: one of
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
dashdash:
dash dash
描述:
表示式 是由運算符和操作數組成的序列,可以指定方法、函式、可寫入的位置或值,描述值的計算過程,產生一或多個副作用,或執行上述功能的某些組合。 例如
- 字面值 123 是表示 int 值 123 的表達式。
- 表達式
1,2,3,4指定顯示值的 4 元素陣列物件。 - 表達式
10.4 * $a會指定計算。 - 表達式
$a++會產生副作用。 - 表達式
$a[$i--] = $b[++$j]會執行這些動作的組合。
除了針對某些運算子所指定之外,表達式中的詞彙評估順序和副作用的發生順序都是未指定的。 未指定行為的範例包括:$i++ + $i、$i + --$i和 $w[$j++] = $v[$j]。
PowerShell 的實作可能會支援使用者定義型別,而這些類型可能已定義它們上的作業。 這類型和操作的所有細節都依據實作來定義。
最上層表達式 是不屬於其他較大表達式的一部分。 如果最上層表達式包含副作用運算符,該表達式的值就不會寫入管線;否則為 。 如需詳細討論,請參閱 §7.1.1。
通常,當使用該表達式的值時,指定集合的表達式([§4§4])會被細分為其組成元素。 不過,當表達式是 Cmdlet 調用時,情況並非如此。 例如
$x = 10,20,30
$a = $($x; 99) # $a.Length is 4
$x = New-Object 'int[]' 3
$a = $($x; 99) # equivalent, $a.Length is 4
$a = $(New-Object 'int[]' 3; 99) # $a.Length is 2
在 $(...) 運算子的前兩個用法中,指定集合的運算式是變數 $x,其列舉會產生三個 int 值,加上 int 99。 不過,第三個案例中,表達式是直接呼叫 Cmdlet,因此不會列舉結果,而 $a 是兩個元素的數位,int[3] 和 int。
如果 PowerShell 未定義作業,則會檢查左操作數所指定的值類型,以查看其是否有對應的 op_<operation> 方法。
7.1 主要運算式
語法:
primary-expression:
value
member-access
element-access
invocation-expression
post-increment-expression
post-decrement-expression
value:
parenthesized-expression
sub-expression
array-expression
script-block-expression
hash-literal-expression
literal
type-literal
variable
7.1.1 群組括號
語法:
提示
語法定義中的 ~opt~ 表示法指出語彙實體在語法中是選擇性的。
parenthesized-expression:
( new-lines~opt~ pipeline new-lines~opt~ )
描述:
括號表達式是 主表達式 其類型和值與沒有括弧的表達式相同。 如果表達式指定變數,則括弧表達式會指定相同的變數。 例如,$x.m 和 ($x).m 相等。
在表達式中,可以使用括號來記錄該表達式內的預設優先順序和關聯性。 它們也可以用來覆寫該預設優先順序和結合性。 例如
4 + 6 * 2 # 16
4 + (6 * 2) # 16 document default precedence
(4 + 6) * 2 # 20 override default precedence
一般而言,最上層的括弧分組是多餘的。 不過,情況不一定如此。 請考慮下列範例:
2,4,6 # Length 3; values 2,4,6
(2,4),6 # Length 2; values [Object[]],int
在第二個案例中,括弧會改變語義,結果是包含兩個元素的陣列:一個是包含兩個整數的陣列,另一個是標量整數 6。
以下是另一個例外狀況:
23.5/2.4 # pipeline gets 9.79166666666667
$a = 1234 * 3.5 # value not written to pipeline
$a # pipeline gets 4319
在第一個和第三個案例中,結果的值會寫入管線。 不過,雖然第二個案例中的表達式已被評估,但結果不會寫入管道,因為最上層存在副作用運算子 =。 (移除 $a = 部分後允許寫入值,因為 * 不是副作用運算符。)
若要停止任何未包含最外層副作用的表達式的值被寫入管線,請明確地將其捨棄,如下所示:
# None of these value are written to pipeline
[void](23.5/2.4)
[void]$a
$null = $a
$a > $null
若要將包含頂層副作用的任何表達式值寫入管線,請將該表達式括在括號中,如下所示:
($a = 1234 * 3.5) # pipeline gets 4319
因此,在此情況下,群組括弧不是多餘的。
在下列範例中,我們在字串文字中進行變數替代(§2.3.5.2):
">$($a = -23)<" # value not written to pipeline, get ><
">$(($a = -23))<" # pipeline gets >-23<
在第一個案例中,括弧代表 子表達式的分隔符,不 分組括弧,而且當最上層表達式包含副作用運算符時,表達式的值不會寫入管線。 當然,仍會書寫 > 和 < 字元。 如果加入括號(如第二個案例所示),即啟用寫入功能。
下列範例各包含最上層副作用運算子:
$a = $b = 0 # value not written to pipeline
$a = ($b = 0) # value not written to pipeline
($a = ($b = 0)) # pipeline gets 0
++$a # value not written to pipeline
(++$b) # pipeline gets 1
$a-- # value not written to pipeline
($b--) # pipeline gets 1
在不含最上層副作用的表達式周圍分組括弧會使這些括弧變得多餘的。 例如;
$a # pipeline gets 0
($a) # no side effect, so () redundant
請考慮以下例子,這個例子有兩個副作用,且這兩個副作用都不在最上層:
12.6 + ($a = 10 - ++$b) # pipeline gets 21.6.
結果會寫入管道,因為頂層表達式沒有副作用。
7.1.2 成員存取
語法:
member-access:
primary-expression . new-line~opt~ member-name
primary-expression :: new-line~opt~ member-name
請注意,主表達式之後,不允許空格符。
描述:
運算子 . 用來選取物件的實例成員或 Hashtable的索引鍵。
左操作數必須指定 物件,而右操作數必須指定可存取的實例成員。
右操作數會指定左操作數所指定物件類型內的可存取實例成員,或者,如果左操作數指定陣列,則右操作數會在數位的每個元素內指定可存取的實例成員。
. 運算子之前不允許空格符。
這個運算子會保持關聯。
運算子 :: 用來從指定類型選取靜態成員。 左操作數必須指定類型,而右側操作數必須指定該類型內可存取的靜態成員。
:: 運算子之前不允許空格符。
這個運算子會保持關聯。
如果右側操作數指定左操作數所指定物件類型內的可寫入位置,則整個表達式會指定可寫入的位置。
例子:
$a = 10, 20, 30
$a.Length # get instance property
(10, 20, 30).Length
$property = "Length"
$a.$property # property name is a variable
$h1 = @{ FirstName = "James"; LastName = "Anderson"; IDNum = 123
}
$h1.FirstName # designates the key FirstName
$h1.Keys # gets the collection of keys
[int]::MinValue # get static property
[double]::PositiveInfinity # get static property
$property = "MinValue"
[long]::$property # property name is a variable
foreach ($t in [byte], [int], [long]) {
$t::MaxValue # get static property
}
$a = @{ID = 1 }, @{ID = 2 }, @{ID = 3 }
$a.ID # get ID from each element in the array
7.1.3 調用表達式
語法:
invocation-expression:
primary-expression . new-line~opt~ member-name argument-list
primary-expression :: new-line~opt~ member-name argument-list
argument-list:
( argument-expression-list~opt~ new-lines~opt~ )
請注意,主表達式之後,不允許空格符。
描述:
調用表示式 會呼叫 primary-expression.member-name 或 primary-expression::member-name所指定的方法。
參數清單中的括號 包含可能為空的逗號分隔的表達式清單,這些表達式指定了 參數,其值將被傳遞給方法。 呼叫 方法之前,會根據 -6的規則來評估自變數,並視需要轉換,以符合 方法所預期的類型。
primary-expression.member-name、primary-expression::member-name和參數的評估順序未指定。
這個運算子會保持關聯。
調用表示式 的結果類型是 方法指示項(4.5.24)。
例子:
[Math]::Sqrt(2.0) # call method with argument 2.0
[char]::IsUpper("a") # call method
$b = "abc#$%XYZabc"
$b.ToUpper() # call instance method
[Math]::Sqrt(2) # convert 2 to 2.0 and call method
[Math]::Sqrt(2D) # convert 2D to 2.0 and call method
[Math]::Sqrt($true) # convert $true to 1.0 and call method
[Math]::Sqrt("20") # convert "20" to 20 and call method
$a = [Math]::Sqrt # get method descriptor for Sqrt
$a.Invoke(2.0) # call Sqrt via the descriptor
$a = [Math]::("Sq"+"rt") # get method descriptor for Sqrt
$a.Invoke(2.0) # call Sqrt via the descriptor
$a = [char]::ToLower # get method descriptor for ToLower
$a.Invoke("X") # call ToLower via the descriptor
7.1.4 元素存取
語法:
element-access:
primary-expression [ new-lines~opt~ expression new-lines~opt~ ]
描述:
主要表達式 與左方括號[之間不應有任何空格符。
7.1.4.1 陣列下標運算
描述:
陣列會在 \9 中詳細討論。 如果 表示式 是一維陣列,請參閱 .7.1.4.5。
當 主運算式 指定一維陣列 A時,運算子 [] 會傳回在 A[0 + expression] 轉換為 之後位於 int 的元素。
結果具有要下標之陣列的元素類型。 如果 表示式 為負數,A[expression] 指定位於 A[A.Length + expression]的元素。
當
當 主運算式 指定三個或多個維度的陣列時,會套用 2 維陣列的規則,並將維度位置指定為逗號分隔的值清單。
如果嘗試對非現有項目進行讀取存取,則結果會 $null。 寫入不存在的元素時發生錯誤。
對於多維度陣列下標表達式,未指定維度位置表達式的評估順序。 例如,假設有 3D 陣列 $a,$a[$i++,$i,++$i] 的行為未指定。
這個運算子會保持關聯。
例子:
$a = [int[]](10,20,30) # [int[]], Length 3
$a[1] # returns int 20
$a[20] # no such position, returns $null
$a[-1] # returns int 30, i.e., $a[$a.Length-1]
$a[2] = 5 # changes int 30 to int 5
$a[20] = 5 # implementation-defined behavior
$a = New-Object 'double[,]' 3,2
$a[0,0] = 10.5 # changes 0.0 to 10.5
$a[0,0]++ # changes 10.5 to 10.6
$list = ("red",$true,10),20,(1.2, "yes")
$list[2][1] # returns string "yes"
$a = @{ A = 10 },@{ B = $true },@{ C = 123.45 }
$a[1]["B"] # $a[1] is a Hashtable, where B is a key
$a = "red","green"
$a[1][4] # returns string "n" from string in $a[1]
如果嘗試對不存在的元素進行寫入操作,就會引發 IndexOutOfRange 例外狀況。
7.1.4.2 下標字串
描述:
當主表達式 指定字串 S時,運算元 [] 會以字元形式傳回 表示式所指示之以零起始位置的字元。 如果 表示式 大於或等於該字串的長度,則結果會 $null。 如果 表示式 為負數,S[expression] 指定位於 S[S.Length + expression]的元素。
例子:
$s = "Hello" # string, Length 5, positions 0-4
$c = $s[1] # returns "e" as a string
$c = $s[20] # no such position, returns $null
$c = $s[-1] # returns "o", i.e., $s[$s.Length-1]
對哈希表取下標
描述:
當 主運算式 指定 Hashtable 時,運算子 [] 會傳回與 表示式所指定之索引鍵相關聯的值。 表示式
當 表示式 為單一索引鍵名稱時,結果會是相關聯的值,而且具有該類型,除非沒有這類索引鍵存在,在此情況下,結果會 $null。 如果 $null 作為鍵使用,則其行為由實作決定。 如果 表達式 是鍵名的陣列,請參閱 §7.1.4.5。
例子:
$h1 = @{ FirstName = "James"; LastName = "Anderson"; IDNum = 123 }
$h1['FirstName'] # the value associated with key FirstName
$h1['BirthDate'] # no such key, returns $null
$h1 = @{ 10 = "James"; 20.5 = "Anderson"; $true = 123 }
$h1[10] # returns value "James" using key 10
$h1[20.5] # returns value "Anderson" using key 20.5
$h1[$true] # returns value 123 using key $true
當 表示式 為單一鍵名稱時,如果 $null 作為 Hashtable 的唯一索引值使用,則會引發 NullArrayIndex 例外狀況。
7.1.4.4 XML 文件的訂閱功能
描述:
當 主表示式 指定 xml 類型的物件時,表達式 會視需要轉換成字元串,而且運算符 [] 傳回第一個子專案,其名稱由 expression所指定。 表示式 $null。 結果未指定任何可寫入的位置。
例子:
$x = [xml]@"
<Name>
<FirstName>Mary</FirstName>
<LastName>King</LastName>
</Name>
"@
$x['Name'] # refers to the element Name
$x['Name']['FirstName'] # refers to the element FirstName within Name
$x['FirstName'] # No such child element at the top level, result is $null
結果的類型為 System.Xml.XmlElement 或 System.String。
7.1.4.5 產生陣列切片
當 主表達式 指定可列舉型別的物件或 Hashtable,且 表達式 是一個一維陣列時,結果是包含 表達式所指定的 主表達式 元素的陣列切片(§9.9)。
在 Hashtable 的情況下,陣列切片會包含提供的索引鍵所對應的關聯值,除非不存在這類索引鍵,在此情況下,對應的元素會是 $null。 如果 $null 被用作任何鍵名稱,則其行為由實作決定。
例子:
$a = [int[]](30,40,50,60,70,80,90)
$a[1,3,5] # slice has Length 3, value 40,60,80
$a[,5] # slice with Length 1
$a[@()] # slice with Length 0
$a[-1..-3] # slice with Length 3, value 90,80,70
$a = New-Object 'int[,]' 3,2
$a[0,0] = 10; $a[0,1] = 20; $a[1,0] = 30
$a[1,1] = 40; $a[2,0] = 50; $a[2,1] = 60
$a[(0,1),(1,0)] # slice with Length 2, value 20,30, parens needed
$h1 = @{ FirstName = "James"; LastName = "Anderson"; IDNum = 123 }
$h1['FirstName'] # the value associated with key FirstName
$h1['BirthDate'] # no such key, returns $null
$h1['FirstName','IDNum'] # returns [Object[]], Length 2 (James/123)
$h1['FirstName','xxx'] # returns [Object[]], Length 2 (James/$null)
$h1[$null,'IDNum'] # returns [Object[]], Length 2 ($null/123)
Windows PowerShell:當 表示式 是兩個或多個索引鍵名稱的集合時,如果 $null 被用作其中一個索引鍵名稱,那麼該索引鍵會被忽略,並且在產生的陣列中沒有對應的元素。
7.1.5 後置遞增和遞減運算符
語法:
post-increment-expression:
primary-expression ++
post-decrement-expression:
primary-expression dashdash
描述:
主運算式 必須指定一個可寫入的位置,此位置應具有數值型別(§4)或值 $null。 如果運算元所指定的值是 $null,則在評估運算符之前,該值會先轉換為 int 型別,然後再轉換為值零。 儲存結果時,主表達式所指定的值類型 可能會變更。 如需討論透過指派的類型變更,請參閱 §7.11。
後置 ++ 運算符所產生的結果是操作數所指定的值。 取得該結果之後,操作數所指定的值會以適當類型的1遞增。
表達式 E++ 結果的類型與表達式 E + 1 的結果相同(#\7.7)。
後置 -- 運算符所產生的結果是操作數所指定的值。 取得該結果之後,操作數所指定的值會遞減為適當類型的 1。
表達式 E-- 結果的類型與表達式 E - 1 的結果相同(#\7.7)。
這些運算子會保持關聯。
例子:
$i = 0 # $i = 0
$i++ # $i is incremented by 1
$j = $i-- # $j takes on the value of $i before the decrement
$a = 1,2,3
$b = 9,8,7
$i = 0
$j = 1
$b[$j--] = $a[$i++] # $b[1] takes on the value of $a[0], then $j is
# decremented, $i incremented
$i = 2147483647 # $i holds a value of type int
$i++ # $i now holds a value of type double because
# 2147483648 is too big to fit in type int
[int]$k = 0 # $k is constrained to int
$k = [int]::MaxValue # $k is set to 2147483647
$k++ # 2147483648 is too big to fit, imp-def behavior
$x = $null # target is unconstrained, $null goes to [int]0
$x++ # value treated as int, 0->1
7.1.6 $(...) 運算符
語法:
sub-expression:
$( new-lines~opt~ statement-list~opt~ new-lines~opt~ )
描述:
如果省略 語句清單,則結果是 $null。 否則,會評估 語句清單。 任何寫入到管線中作為評估一部分的物件,會依序收集在無限制的 1 維陣列中。 如果所收集物件的陣列是空的,則結果會 $null。 如果所收集物件的陣列包含單一元素,結果就是該元素;否則,結果是所收集結果不受限制的 1 維陣列。
例子:
$j = 20
$($i = 10) # pipeline gets nothing
$(($i = 10)) # pipeline gets int 10
$($i = 10; $j) # pipeline gets int 20
$(($i = 10); $j) # pipeline gets [Object[]](10,20)
$(($i = 10); ++$j) # pipeline gets int 10
$(($i = 10); (++$j)) # pipeline gets [Object[]](10,22)
$($i = 10; ++$j) # pipeline gets nothing
$(2,4,6) # pipeline gets [Object[]](2,4,6)
7.1.7 @(...) 運算符
語法:
array-expression:
@( new-lines~opt~ statement-list~opt~ new-lines~opt~ )
描述:
如果省略 語句清單,則結果是長度為零的不受限制的 1D 陣列。 否則,會評估 語句清單,而在評估過程中寫入管線的任何物件,會依序收集在一個無限制的 1 維陣列中。 結果是無限制的 1 維陣列(可能空白)。
例子:
$j = 20
@($i = 10) # 10 not written to pipeline, result is array of 0
@(($i = 10)) # pipeline gets 10, result is array of 1
@($i = 10; $j) # 10 not written to pipeline, result is array of 1
@(($i = 10); $j) # pipeline gets 10, result is array of 2
@(($i = 10); ++$j) # pipeline gets 10, result is array of 1
@(($i = 10); (++$j)) # pipeline gets both values, result is array of 2
@($i = 10; ++$j) # pipeline gets nothing, result is array of 0
$a = @(2,4,6) # result is array of 3
@($a) # result is the same array of 3
@(@($a)) # result is the same array of 3
7.1.8 腳本區塊表達式
語法:
script-block-expression:
{ new-lines~opt~ script-block new-lines~opt~ }
script-block:
param-block~opt~ statement-terminators~opt~ script-block-body~opt~
script-block-body:
named-block-list
statement-list
描述:
param-block 在 §8.10.9中描述。 named-block-list 在 §8.10.7中說明。
腳本區塊是可做為單一單位的未命名語句區塊。 腳本區塊可用來叫用程式代碼區塊,就像是單一命令一樣,也可以指派給可執行的變數。
命名區塊清單 或 語句清單 執行後,其結果的類型和值是這些語句集結果的類型和值。
script-block-expression 具有類型 scriptblock(§4.3.7)。
如果省略 param-block,則任何傳遞至腳本區塊的自變數都可透過 $args 取得(#\8.10.1)。
在參數系結期間,腳本區塊可以傳遞為腳本區塊物件,或在評估腳本區塊之後作為結果傳遞。 如需詳細資訊,請參閱 §6.17。
7.1.9 哈希常值表達式
語法:
hash-literal-expression:
@{ new-lines~opt~ hash-literal-body~opt~ new-lines~opt~ }
hash-literal-body:
hash-entry
hash-literal-body statement-terminators hash-entry
hash-entry:
key-expression = new-lines~opt~ statement
key-expression:
simple-name
unary-expression
statement-terminators:
statement-terminator
statement-terminators statement-terminator
statement-terminator:
;
new-line-character
描述:
hash-literal-expression 用來建立包含零個或多個元素的哈希表(§10),每個元素都是一個鍵/值對。
索引鍵可能具有 Null 類型以外的任何類型。 相關聯的值可能具有任何類型,包括 null 型別,而且每個值都可以是任何能夠指示所需值的運算式,包括 $null。
索引鍵/值組的順序並不重要。
例子:
$h1 = @{ FirstName = "James"; LastName = "Anderson"; IDNum = 123 }
$last = "Anderson"; $IDNum = 120
$h2 = @{ FirstName = "James"; LastName = $last; IDNum = $IDNum + 3 }
$h3 = @{ }
$h4 = @{ 10 = "James"; 20.5 = "Anderson"; $true = 123 }
會建立兩個哈希表,$h1 和 $h2,每個哈希表都包含三個索引鍵/值組,而第三個索引鍵/值組則為空白 $h3。 哈希表 $h4 具有各種類型的索引鍵。
7.1.10 類型常值表達式
語法:
type-literal:
[ type-spec ]
type-spec:
array-type-name new-lines~opt~ dimension~opt~ ]
generic-type-name new-lines~opt~ generic-type-arguments ]
type-name
dimension:
,
dimension ,
generic-type-arguments:
type-spec new-lines~opt~
generic-type-arguments , new-lines~opt~ type-spec
array-type-name:
type-name [
generic-type-name:
type-name [
描述:
類型常值 在實作中是由某些未指定的 基礎類型來表示。 因此,類型名稱與其基礎類型同義。
類型常值被用於許多情境中:
例子:
[int].IsPrimitive # $true
[Object[]].FullName # "System.Object[]"
[int[,,]].GetArrayRank() # 3
專門用來保存字串的泛型堆疊類型 (!4.4)可能會寫入為 [Stack[string]],而專門用來 int 保存具有相關聯字串值的索引鍵的泛型字典類型,可能會寫入為 [Dictionary[int,string]]。
類型文字常值 的類型是 System.Type。 以上建議的類型 Stack[string] 的完整名稱是 System.Collections.Generic.Stack[int]。 以上建議的類型 Dictionary[int,string] 的完整名稱是 System.Collections.Generic.Dictionary[int,string]。
7.2 一元運算符
語法:
unary-expression:
primary-expression
expression-with-unary-operator
expression-with-unary-operator:
, new-lines~opt~ unary-expression
-not new-lines~opt~ unary-expression
! new-lines~opt~ unary-expression
-bnot new-lines~opt~ unary-expression
+ new-lines~opt~ unary-expression
dash new-lines~opt~ unary-expression
pre-increment-expression
pre-decrement-expression
cast-expression
-split new-lines~opt~ unary-expression
-join new-lines~opt~ unary-expression
pre-increment-expression:
++ new-lines~opt~ unary-expression
pre-decrement-expression:
dashdash new-lines~opt~ unary-expression
dash: one of
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
cast-expression:
type-literal unary-expression
dashdash:
dash dash
7.2.1 一元逗號運算符
描述:
逗號運算子(,)會建立具有一個元素的不受限制的一維陣列,其類型和值是一元運算式。
這個運算符是右結合的。
例子:
$a = ,10 # create an unconstrained array of 1 element, $a[0],
# which has type int
$a = ,(10,"red") # create an unconstrained array of 1 element,
$a[0],
# which is an unconstrained array of 2 elements,
# $a[0][0] an int, and $a[0][1] a string
$a = ,,10 # create an unconstrained array of 1 element, which is
# an unconstrained array of 1 element, which is an int
# $a[0][0] is the int. Contrast this with @(@(10))
7.2.2 邏輯非
語法:
logical-not-operator:
dash not
dash: one of
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
運算子 ! 是 -not的另一種拼法。
這個運算符是右結合的。
例子:
-not $true # False
-not -not $false # False
-not 0 # True
-not 1.23 # False
!"xyz" # False
7.2.3 位元 NOT 運算
語法:
bitwise-not-operator:
dash bnot
dash: one of
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
運算子 -bnot 在必要時將由 一元運算式 指定的值轉換為整數類型(§6.4)。 如果轉換的值可以在 int int 中表示,則為結果類型。 否則,如果轉換的值可以用類型 長 表示,則該值的結果類型即為此類型。 否則,表達式的格式不正確。 產生的值是已轉換值的一補數。
這個運算符是右結合的。
例子:
-bnot $true # int with value 0xFFFFFFFE
-bnot 10 # int with value 0xFFFFFFF5
-bnot 2147483648.1 # long with value 0xFFFFFFFF7FFFFFFF
-bnot $null # int with value 0xFFFFFFFF
-bnot "0xabc" # int with value 0xFFFFF543
7.2.4 一元加號
描述:
運算式 + unary-expression 被視為 0 + unary-expression(§7.7)。 整數常值 0 的類型 int。
這個運算符是右結合的。
例子:
+123L # type long, value 123
+0.12340D # type decimal, value 0.12340
+"0xabc" # type int, value 2748
7.2.5 一元減號
描述:
運算式 - unary-expression 被視為 0 - unary-expression(§7.7)。 整數字面值 0 的類型是 int。 減號運算符可以是 字元中的任一個,這些字元列在 §7.2中。
這個運算符是右結合的。
例子:
-$true # type int, value -1
-123L # type long, value -123
-0.12340D # type decimal, value -0.12340
7.2.6 前置詞遞增和遞減運算符
描述:
一元表示式 必須指向一個可寫入的位置,該位置要有數值類型的值(§4)或值 $null。 如果其 一元表達式所指定的值 為 $null,一元表達式的值會轉換成 int 類型,並在評估運算符之前將值為零。
注意
儲存結果時,一元表達式所指定的值類型 可能會變更。 如需討論透過指派的類型變更,請參閱 §7.11。
針對前置詞遞增運算符 ++ ,一元表達式 的值會依適當類型的 1 遞增。 結果是已進行遞增後的新值。 表達式 ++E 相當於 E += 1 (7.11.2)。
針對前置詞遞減運算符 -- ,一元表達式 的值會由適當類型的 1 遞減。 遞減後得到的新值就是結果。 表達式 --E 相當於 E -= 1 (7.11.2)。 前綴遞減運算符可以是符合 dashdash 模式的任何模式,正如 §7.2中所描述的。
這些運算子是右結合性。
例子:
$i = 0 # $i = 0
++$i # $i is incremented by 1
$j = --$i # $i is decremented then $j takes on the value of $i
$a = 1,2,3
$b = 9,8,7
$i = 0;
$j = 1
$b[--$j] = $a[++$i] # $j is # decremented, $i incremented, then $b[0]
# takes on the value of $a[1]
$i = 2147483647 # $i holds a value of type int
++$i # $i now holds a value of type double because
# 2147483648 is too big to fit in type int
[int]$k = 0 # $k is constrained to int
$k = [int]::MinValue # $k is set to -2147483648
--$k # -2147483649 is too small to fit, imp-def behavior
$x = $null # target is unconstrained, $null goes to [int]0
--$x # value treated as int, 0 becomes -1
7.2.7 一元 -join 運算符
語法:
join-operator:
dash join
dash: one of
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
一元 -join 運算符會生成一個字串,該字串是由 一元表達式指定的一個或多個物件的值串連而成。 (您可以使用這個運算符的二進位版本插入分隔符 (7.8.4.4)。
一元表達式 可以是純量值或集合。
例子:
-join (10, 20, 30) # result is "102030"
-join (123, $false, 19.34e17) # result is "123False1.934E+18"
-join 12345 # result is "12345"
-join $null # result is ""
7.2.8 一元 -split 運算符
語法:
split-operator:
dash split
dash: one of
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
一元 -split 運算符會將 一元表示式所指定的一或多個字符串進行分割,並將其子部分傳回至受限制的 1 維字串陣列中。 它會將任何連續的空格符群組視為後續子部分之間的分隔符。 您可以使用這個運算符的二進位版本來指定明確的分隔符字串(7.8.4.5)或其兩個變體()。。
產生的字串中不包含分隔符文字。 會忽略輸入字串中的前置和尾端空格符。 空白或包含空格符的輸入字串只會產生一個字串的陣列,這是空的。
一元表示式 可以指定純量值或字串陣列。
例子:
-split " red`tblue`ngreen " # 3 strings: "red", "blue", "green"
-split ("yes no", "up down") # 4 strings: "yes", "no", "up", "down"
-split " " # 1 (empty) string
7.2.9 轉換運算子
描述:
此運算子明確地將 一元表示式 所指定的值轉換為 類型字面值 所指定的類型(§7.1.10)。 如果 類型字面值 不是 void,則結果的類型為具名類型,而數值則是經過轉換後的值。 如果 類型常值 為 void,則不會將物件寫入管線,而且沒有結果。
當任何型別的表達式被轉換成該相同型別時,產生的型別和值是一元表達式
這個運算符是右結合的。
例子:
[bool]-10 # a bool with value True
[int]-10.70D # a decimal with value -10
[int]10.7 # an int with value 11
[long]"+2.3e+3" # a long with value 2300
[char[]]"Hello" # an array of 5 char with values H, e, l, l, and o.
7.3 二進制逗號運算符
語法:
array-literal-expression:
unary-expression , new-lines~opt~ array-literal-expression
描述:
雙元逗號運算子會建立一個一維陣列,其元素值按字典順序由其操作數指定。 陣列具有不受限制的類型。
例子:
2,4,6 # Length 3; values 2,4,6
(2,4),6 # Length 2; values [Object[]],int
(2,4,6),12,(2..4) # Length 3; [Object[]],int,[Object[]]
2,4,6,"red",$null,$true # Length 6
將括弧加到特定二進位逗號表達式並不會記錄預設優先順序;相反地,它會變更結果。
7.4 範圍運算子
語法:
range-expression:
unary-expression .. new-lines~opt~ unary-expression
描述:
range-expression 會建立一個不受限制的單維陣列,其元素是由範圍界限指定的 int 序列的值。 操作數所指定的值會視需要轉換成int(§6.4)。 轉換后指定較低值的操作數是 下限,而轉換后指定較高值的操作數是 上限。 這兩個界限可能相同,在此情況下,產生的陣列長度 1。
如果左操作數指定下限,序列會以遞增順序排列。 如果左操作數指定上限,序列會依遞減順序排列。
就概念而言,這個運算符是對應二進位逗號運算符序列的快捷方式。 例如,也可以使用 5..8來產生範圍 5,6,7,8。 不過,如果需要遞增或遞減序列而不需要陣列,則實作可能會避免產生實際的陣列。 例如,在 foreach ($i in 1..5) { ... }中,不需要建立陣列。
範圍表示式 可用來指定陣列切片(§9.9)。
例子:
1..10 # ascending range 1..10
-495..-500 # descending range -495..-500
16..16 # sequence of 1
$x = 1.5
$x..5.40D # ascending range 2..5
$true..3 # ascending range 1..3
-2..$null # ascending range -2..0
0xf..0xa # descending range 15..10
7.5 格式運算符
語法:
format-expression:
format-specification-string format-operator new-lines~opt~ range-expression
format-operator:
dash f
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
format-expression 會根據 format-expression所指定的 format-specification-string,格式化 range-expression 所指定的一或多個值。
range-expression 所指定值的位置會以零開始編號,並以語彙順序遞增。 結果的類型 string。
格式規格字串可能包含零個或多個格式規格,每個格式都有下列格式:
{N [ ,M ][ : FormatString ]}
N 代表 範圍表示式 值位置,M 代表最小顯示寬度,FormatString 表示 (選擇性) 格式。 如果格式化值的寬度超過指定的寬度,則寬度會隨之增加。 未在 formatString 中參考其位置的數值,在評估任何副作用後會被忽略。 如果 N 指的是不存在的位置,則其行為依照實作的定義而定。 類型為 $null 和 void 的值會格式化為空字串。 陣列的格式化為 子表示式 (§7.1.6)。 若要在格式規格中包含字元 { 和 },而不將其解譯為格式分隔符,請分別將它們寫入為 {{ 和 }}。
如需格式規格的完整定義,請參閱 System.IFormattable中的類型 。
例子:
"__{0,3}__" -f 5 # __ 5__
"__{0,-3}__" -f 5 # __5 __
"__{0,3:000}__" -f 5 # __005__
"__{0,5:0.00}__" -f 5.0 # __ 5.00__
"__{0:C}__" -f 1234567.888 # __$1,234,567.89__
"__{0:C}__" -f -1234.56 # __($1,234.56)__
"__{0,12:e2}__" -f 123.456e2 # __ 1.23e+004__
"__{0,-12:p}__" -f -0.252 # __-25.20 % __
$i = 5; $j = 3
"__{0} + {1} <= {2}__" -f $i,$j,($i+$j) # __5 + 3 <= 8__
$format = "__0x{0:X8}__"
$format -f 65535 # __0x0000FFFF__
在格式規格中,如果 N 參考不存在的位置,則會引發 FormatError。
7.6 乘法運算符
語法:
multiplicative-expression:
multiplicative-expression * new-lines~opt~ format-expression
multiplicative-expression / new-lines~opt~ format-expression
multiplicative-expression % new-lines~opt~ format-expression
7.6.1 乘法
描述:
乘法運算子 * 的結果,是套用一般算術轉換之後,兩個操作數所指定的值乘積(•6.15)。
這個運算子會保持關聯。
例子:
12 * -10L # long result -120
-10.300D * 12 # decimal result -123.600
10.6 * 12 # double result 127.2
12 * "0xabc" # int result 32976
7.6.2 字串複寫
描述:
當左操作數是字串時,位元運算 * 運算子會建立一個新字串,該字串內容為左操作數所指定的字串被重複若干次,重複次數由右操作數的值轉換為整數類型時所指定(§6.4)。
這個運算子會保持關聯。
例子:
"red" * "3" # string replicated 3 times
"red" * 4 # string replicated 4 times
"red" * 0 # results in an empty string
"red" * 2.3450D # string replicated twice
"red" * 2.7 # string replicated 3 times
7.6.3 陣組複寫
描述:
當左操作數指定陣列時,二元運算子 * 會建立一個新的未受約束的 1 維陣列,其中包含由左操作數指定的值,並根據右操作數的值轉換為整數類型後指定的次數重複出現(§6.4)。 復寫計數為零會導致長度為1的陣列。 如果左操作數指定多維陣列,則在使用前會被扁平化(§9.12)。
這個運算子會保持關聯。
例子:
$a = [int[]](10,20) # [int[]], Length 2*1
$a * "3" # [Object[]], Length 2*3
$a * 4 # [Object[]], Length 2*4
$a * 0 # [Object[]], Length 2*0
$a * 2.3450D # [Object[]], Length 2*2
$a * 2.7 # [Object[]], Length 2*3
(New-Object 'float[,]' 2,3) * 2 # [Object[]], Length 2*2
7.6.4 除法
描述:
使用除法運算子 / 的結果是當左操作數所指定的值除以右操作數所指定的值後得到的商,其中套用了通常的算術轉換(§6.15)。
如果嘗試以零執行整數或十進位除法,則會引發實作定義的終止錯誤。
這個運算子會保持關聯。
例子:
10/-10 # int result -1
12/-10 # double result -1.2
12/-10D # decimal result 1.2
12/10.6 # double result 1.13207547169811
12/"0xabc" # double result 0.00436681222707424
如果嘗試以零執行整數或十進位除法,則會引發 RuntimeException 例外狀況。
7.6.5 餘數
描述:
餘數運算符 % 的結果是在經過一般算術轉換(§6.15)後,由左操作數指定的值除以右操作數指定的值時所得的餘數。
如果嘗試以零執行整數或十進位除法,則會引發實作定義的終止錯誤。
例子:
10 % 3 # int result 1
10.0 % 0.3 # double result 0.1
10.00D % "0x4" # decimal result 2.00
如果嘗試以零執行整數或十進位除法,則會引發 RuntimeException 例外狀況。
7.7 加法運算符
語法:
additive-expression:
primary-expression + new-lines~opt~ expression
primary-expression dash new-lines~opt~ expression
dash: one of
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
7.7.1 加法
描述:
加法運算子 + 的結果是套用一般算術轉換之後,兩個操作數所指定的值總和(•6.15)。
這個運算子會保持關聯。
例子:
12 + -10L # long result 2
-10.300D + 12 # decimal result 1.700
10.6 + 12 # double result 22.6
12 + "0xabc" # int result 2760
7.7.2 字串串連
描述:
當左操作數指定字串時,二進位 + 運算子會建立新的字串,其中包含左操作數所指定的值,後面緊接著右操作數所指定的值,並轉換為類型字串 (§6.8)。
這個運算子會保持關聯。
例子:
"red" + "blue" # "redblue"
"red" + "123" # "red123"
"red" + 123 # "red123"
"red" + 123.456e+5 # "red12345600"
"red" + (20,30,40) # "red20 30 40"
7.7.3 陣列串連
描述:
當左操作數指定陣列時,二進位 + 運算符會建立新的不受限制的 1 維陣列,其中包含左操作數所指定的元素,後面緊接右操作數所指定的值。 使用前,任一操作數中的多維陣列會被扁平化(§9.12)。
這個運算子會保持關聯。
例子:
$a = [int[]](10,20) # [int[]], Length 2
$a + "red" # [Object[]], Length 3
$a + 12.5,$true # [Object[]], Length 4
$a + (New-Object 'float[,]' 2,3) # [Object[]], Length 8
(New-Object 'float[,]' 2,3) + $a # [Object[]], Length 8
7.7.4 哈希表串連
描述:
當這兩個操作數指定 Hashtable 時,二進位 + 運算符會建立新的 Hashtable,其中包含由左操作數所指定的元素緊接右操作數所指定的元素。
如果 Hashtable 包含相同的索引鍵,則會引發實作定義的終止錯誤。
這個運算子會保持關聯。
例子:
$h1 = @{ FirstName = "James"; LastName = "Anderson" }
$h2 = @{ Dept = "Personnel" }
$h3 = $h1 + $h2 # new Hashtable, Count = 3
如果 Hashtable 包含相同的索引鍵,則會引發 BadOperatorArgument 類型的例外狀況。
7.7.5 減法
描述:
減法運算子的結果 - 是在套用一般算術轉換之後,從左操作數所指定的值減去右操作數所指定的值時的差異(\6.15)。 減法運算子可以是列於
這個運算子會保持關聯。
例子:
12 - -10L # long result 22
-10.300D - 12 # decimal result -22.300
10.6 - 12 # double result -1.4
12 - "0xabc" # int result -2736
7.8 比較運算符
語法:
comparison-expression:
primary-expression comparison-operator new-lines~opt~ expression
comparison-operator:
equality-operator
relational-operator
containment-operator
type-operator
like-operator
match-operator
描述:
左操作數所指定的值型別,會視需要決定右操作數所指定的值轉換方式(\6),然後才完成比較。
有些比較運算符有兩個變體,一個是區分大小寫(-c<operator>),另一個不區分大小寫(-i<operator>)。
-<operator> 版本相當於 -i<operator>。 區分大小寫只有在字串類型的值比較時才有意義。 在非字串比較內容中,兩個變體的行為相同。
這些運算子會保持關聯。
7.8.1 相等和關係運算符
語法:
equality-operator: one of
dash eq dash ceq dash ieq
dash ne dash cne dash ine
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
relational-operator: one of
dash lt dash clt dash ilt
dash le dash cle dash ile
dash gt dash cgt dash igt
dash ge dash cge dash ige
描述:
有兩個相等運算符:平等(-eq)和不平等(-ne):和四個關係運算符:小於 (-lt)、小於或等於 (-le)、大於 (-gt),以及大於或等於 (-ge)。 每一個都有兩個變體(#7.8)。
若要讓兩個字串比較相等,它們必須有相同的長度和內容,以及適當情況下的字母大小寫。
如果左操作數指定的值不是集合,則結果的類型 bool。
否則,結果可能是空且不受限制的一維陣列,包含集合中在與右操作數指定的值比較時測試為 True 的元素。
例子:
10 -eq "010" # True, int comparison
"010" -eq 10 # False, string comparison
"RED" -eq "Red" # True, case-insensitive comparison
"RED" -ceq "Red" # False, case-sensitive comparison
"ab" -lt "abc" # True
10,20,30,20,10 -ne 20 # 10,30,10, Length 3
10,20,30,20,10 -eq 40 # Length 0
10,20,30,20,10 -ne 40 # 10,20,30,20,10, Length 5
10,20,30,20,10 -gt 25 # 30, Length 1
0,1,30 -ne $true # 0,30, Length 2
0,"00" -eq "0" # 0 (int), Length 1
7.8.2 內含項目運算符
語法:
containment-operator: one of
dash contains dash ccontains dash icontains
dash notcontains dash cnotcontains dash inotcontains
dash in dash cin dash iin
dash notin dash cnotin dash inotin
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
有四個內含運算符:包含(-contains)、不包含(-notcontains)、在內(-in)、和不在內(-notin)。 每一個都有兩個變體(#7.8)。
內含項目運算符會傳回bool類型的結果,指出值是否在陣列的元素中至少發生一次(或未發生)。 使用 -contains 和 -notcontains時,值是由右操作數指定,而陣列是由左操作數所指定。 使用 -in 和 -notin,操作數會反轉。 值是由左操作數所指定,而陣列是由右操作數所指定。
針對這些運算子的目的,如果數組操作數具有標量值,則該標量值會被視為一個元素的數組。
例子:
10,20,30,20,10 -contains 20 # True
10,20,30,20,10 -contains 42.9 # False
10,20,30 -contains "10" # True
"10",20,30 -contains 10 # True
"010",20,30 -contains 10 # False
10,20,30,20,10 -notcontains 15 # True
"Red",20,30 -ccontains "RED" # False
7.8.3 類型測試和轉換運算符
語法:
type-operator: one of
dash is
dash as
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
類型運算子 -is 用來測試由左操作元指定的值是否具有右操作元指定的型別,或是否從該型別衍生。 右操作數必須指定類型或值,才能轉換成類型(例如為型別命名的字串)。
結果類型為 bool。 類型運算子 -isnot 傳回對應 -is 結構的邏輯否定。
類型運算子 -as 嘗試將左操作數所指定的值轉換為右操作數所指定的類型。 右操作數必須指定類型或值,才能轉換成類型(例如為型別命名的字串)。 如果轉換失敗,則會傳回 $null;否則,會傳回轉換的值,而該結果的傳回型別是已轉換值的運行時間類型。
例子:
$a = 10 # value 10 has type int
$a -is [int] # True
$t = [int]
$a -isnot $t # False
$a -is "int" # True
$a -isnot [double] # True
$x = [int[]](10,20)
$x -is [int[]] # True
$a = "abcd" # string is derived from object
$a -is [Object] # True
$x = [double]
foreach ($t in [int],$x,[decimal],"string") {
$b = (10.60D -as $t) * 2 # results in int 22, double 21.2
} # decimal 21.20, and string "10.6010.60"
7.8.4 模式比對和文字操作運算符
7.8.4.1 -like 和 -notlike 運算符
語法:
like-operator: one of
dash like dash clike dash ilike
dash notlike dash cnotlike dash inotlike
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
如果左操作數未指定集合,則結果的類型為 bool。 否則,結果可能是空且不受限制的一維陣列,包含集合中在與右操作數指定的值比較時測試為 True 的元素。 右操作數可以指定包含通配符表達式的字串(§3.15)。 這些運算子有兩個變體(§7.8)。
例子:
"Hello" -like "h*" # True, starts with h
"Hello" -clike "h*" # False, does not start with lowercase h
"Hello" -like "*l*" # True, has an l in it somewhere
"Hello" -like "??l" # False, no length match
"-abc" -like "[-xz]*" # True, - is not a range separator
"#$%\^&" -notlike "*[A-Za-z]" # True, does not end with alphabetic character
"He" -like "h[aeiou]?*" # False, need at least 3 characters
"When" -like "*[?]" # False, ? is not a wildcard character
"When?" -like "*[?]" # True, ? is not a wildcard character
"abc","abbcde","abcgh" -like "abc*" # object[2], values
"abc" and "abcgh"
7.8.4.2 -match 和 -notmatch 運算符
語法:
match-operator: one of
dash match dash cmatch dash imatch
dash notmatch dash cnotmatch dash inotmatch
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
如果左操作數未指定集合,則結果的類型為 bool,且該結果為 $true,則 Hashtable $Matches 的項目會設定為符合右操作數所指定值的字串(或不相符)。 否則,結果可能是空的未受限制的 1D 陣列,其中包含集合中與右操作數指定的值進行比較後結果為 True 的元素,而且 $Matches 未設定。 右操作數可以指定包含正則表達式的字串(\3.16),在此情況下,其稱為 模式。 這些運算子有兩個變體(§7.8)。
這些運算子支援子匹配(§7.8.4.6)。
例子:
"Hello" -match ".l" # True, $Matches key/value is 0/"el"
"Hello" -match '\^h.*o$' # True, $Matches key/value is
0/"Hello"
"Hello" -cmatch '\^h.*o$' # False, $Matches not set
"abc\^ef" -match ".\\\^e" # True, $Matches key/value is 0/"c\^e"
"abc" -notmatch "[A-Za-z]" # False
"abc" -match "[\^A-Za-z]" # False
"He" -match "h[aeiou]." # False, need at least 3 characters
"abc","abbcde","abcgh" -match "abc.*" # Length is 2, values "abc", "abcgh"
7.8.4.3 -replace 運算符
語法:
binary-replace-operator: one of
dash replace dash creplace dash ireplace
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
-replace 運算符允許使用右操作數所指定的值,在左操作數所指定的一或多個字元串中取代文字。 此運算子有兩個變體(§7.8)。 右操作數具有下列其中一種形式:
- 要找到的字串,其中可能包含正則表示式 (§3.16)。 在此情況下,取代字串會隱含為 “ ”
- 包含兩個物件的陣列,其中包含要找到的字串,接著是取代的字串。
如果左操作數指定字串,結果就會有類型字串。 如果左操作數指定字串的 1 維陣列,則結果為不受限制的 1 維陣列,其長度與左操作數陣列相同,包含完成替換後的輸入字串。
此運算子支援子匹配(§7.8.4.6)。
例子:
"Analogous","an apple" -replace "a","*" # "*n*logous","*n *pple"
"Analogous" -creplace "[aeiou]","?" # "An?l?g??s"
"Analogous","an apple" -replace '\^a',"%%A" # "%%Analogous","%%An apple"
"Analogous" -replace "[aeiou]",'$&$&' # "AAnaaloogoouus"
7.8.4.4 二進位 -join 運算符
語法:
binary-join-operator: one of
dash join
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
二進位 -join 運算符會產生字串,此字串是一或多個由左操作數指定之物件的串連,必要時轉換為字串串 ()。。 由右操作數指定的字串是用來分隔產生的字串中的 (可能空白) 值。
左操作數可以是純量值或集合。
例子:
(10, 20, 30) -join "\|" # result is "10\|20\|30"
12345 -join "," # result is "12345", no separator needed
($null,$null) -join "<->" # result is "<->", two zero-length values
7.8.4.5 二元運算符 "-split"
語法:
binary-split-operator: one of
dash split dash csplit dash isplit
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
二元 -split 運算符會分割左操作數所指定的一或多個字串,並在受約束的 1 維字串陣列中傳回其子字串。 此運算子有兩個變體(§7.8)。 左操作數可以指定純量值或字串陣列。 右操作數具有下列其中一種形式:
- 分隔符字串
- 包含分隔符字串的 2 個物件陣列,後面接著數值 分割計數
- 3 個物件的陣列,每個物件包含一個分隔符字串、一個數值分割計數,以及一個 選項字串。
- 腳本區塊
- 包含一個腳本區塊和一個數值分割計數的兩個物件組成陣列
分隔符字串可能包含正規表示式(§3.16)。 它用來尋找具有輸入字串的子元件。 產生的字串中不包含分隔符。 如果左操作數指定空字串,則會導致空字串元素。 如果分隔符字串是空字串,則會在輸入字串中的每個字元位置找到它。
根據預設,輸入字串的所有子字串都會以個別元素的形式放入結果中;不過,可以使用分割次數來修改此行為。 如果該計數為負數、零或大於或等於輸入字串中的子部分數目,則每個子元件都會進入個別元素。 如果該計數小於輸入字串中的子部分數量,則結果中有 count 個元素,最後一個元素包含超過前 count - 1 個 子部分的所有子部分。
選項字串包含零個或多個 選項名稱, 每個相鄰配對以逗號分隔。 會忽略前後和嵌入的空白字符。 選項名稱可能依任何順序排列,且區分大小寫。
如果 options 字串包含選項名稱 SimpleMatch,它也可能包含選項名稱 IgnoreCase。 如果選項字串包含選項名稱 RegexMatch,或不包含 RegexMatch 或 SimpleMatch,則它可能包含除了 SimpleMatch以外的任何選項名稱。 不過,它不能同時包含 多行 和 單行。
以下是一組選項名稱:
| 選項 | 描述 |
|---|---|
| 文化不變性 | 評估分隔符時,忽略語言的文化差異。 |
| ExplicitCapture | 忽略非具名比對群組,因此只會在結果清單中傳回明確的擷取群組。 |
| IgnoreCase | 強制不區分大小寫比對,即使使用 -csplit 也一樣。 |
| 忽略模式空白 (IgnorePatternWhitespace) | 忽略未轉義的空白符和以數字符號標記的註解(#)。 |
| 多行 | 此模式可辨識行和字串的開頭和結尾。 預設模式 單行。 |
| RegexMatch | 使用正則表達式比對來評估分隔符。 這是預設值。 |
| SimpleMatch | 評估分隔符時,請使用簡單的字串比較。 |
| 單行 | 此模式只會辨識字串的開頭和結尾。 這是預設模式。 |
腳本區塊 (\7.1.8) 會指定判斷分隔符的規則,而且必須評估為 bool 類型。
例子:
"one,forty two,," -split "," # 5 strings: "one" "forty two" "" ""
"abc","de" -split "" # 9 strings: "" "a" "b" "c" "" "" "d" "e" ""
"ab,cd","1,5,7,8" -split ",", 2 # 4 strings: "ab" "cd" "1" "5,7,8"
"10X20x30" -csplit "X", 0, "SimpleMatch" # 2 strings: "10" "20x30"
"analogous" -split "[AEIOU]", 0, "RegexMatch, IgnoreCase"
# 6 strings: "" "n" "l" "g" "" "s"
"analogous" -split { $_ -eq "a" -or $_ -eq "o" }, 4
# 4 strings: "" "n" "l" "gous"
7.8.4.6 子匹配
由 -match、-notmatch和 -replace 比對的模式可能包含以括弧分隔的子部分(稱為 子匹配)。 請考慮下列範例:
"red" -match "red"
結果是 $true,且 $Matches 索引鍵 0 包含「red」,該部分的字串由左操作數指定,完全符合右操作數指定的模式。
在下列範例中,整個樣式是子匹配:
"red" -match "(red)"
如前所述,索引鍵 0 包含「red」;不過,索引鍵 1 也包含「red」,這是由左操作數指定的精確匹配子匹配的字串部分。
請考慮下列更複雜的模式:
"red" -match "((r)e)(d)"
此模式允許「re」、「r」、「d」或「red」的子匹配。
同樣地,鍵 0 包含「red」。 密鑰 1 包含 「re」 金鑰 2 包含 「r」 ,而金鑰 3 則包含 「d」。 鍵/值對依照模式從左到右匹配,且較長的字串匹配優先於較短的字串。
在 -replace的情況下,替換文字可以透過類似 $n形式的名稱存取子比對項,其中第一個比對是 $1,第二個是 $3,以此類推。 例如
"Monday morning" -replace '(Monday|Tuesday) (morning|afternoon|evening)','the $2 of $1'
產生的字串為「星期一上午」。
子專案可以使用格式 $Matches來命名,而不是在 ?<*name*> 中使用從零開始的索引作為鍵。 例如,"((r)e)(d)" 可以使用三個具名的子匹配來撰寫,m1、m2和 m3,如下所示:"(?<m1>(?<m2>r)e)(?<m3>d)"。
7.8.5 位移運算符
語法:
shift-operator: one of
dash shl
dash shr
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
向左移(-shl)運算符和向右移(-shr)運算符會將左運算元所指定的值轉換成整數類型,並在必要時,使用一般算術轉換將右運算元指定的值轉換為整數(§6.15)。
左移運算符會將左邊的操作數按照如下所述的計算方式左移指定的位數。 低序空位位置會設定為零。
右移運算子會將左操作數向右移動一定數量的位元,這個數量是按照以下描述計算的。 左操作數的低序位會被捨棄,其餘位則向右移位。 當左操作數是帶正負號值時,如果左操作數為非負數,則高階空位位置會設定為零,如果左操作數為負數,則設定為一個。 當左操作數是無符號值時,高階空位位置會設定為零。
當左運算元類型為 int 時,位移計數由右運算元的低階五位元提供。 當右操作數的類型為long時,右操作數的最低六位決定移位計數。
例子:
0x0408 -shl 1 # int with value 0x0810
0x0408 -shr 3 # int with value 0x0081
0x100000000 -shr 0xfff81 # long with value 0x80000000
7.9 位運算符
語法:
bitwise-expression:
unary-expression -band new-lines~opt~ unary-expression
unary-expression -bor new-lines~opt~ unary-expression
unary-expression -bxor new-lines~opt~ unary-expression
描述:
位元 AND 運算子 -band、位元 OR 運算子 -bor,以及位元 XOR 運算子 -bxor 若有需要,可使用一般算術轉換 (§6.15)將其運算元指定的值轉換為整數類型。 轉換之後,如果兩個值都有類型 int,即為結果的類型。 否則,如果兩個值都是 long 型別,那麼結果也是這個型別。
如果某個值的類型 int,而另一個值的類型為long,則結果的類型為long。
否則,表達式的格式不正確。 結果是可能被轉換的操作數值的位元 AND、位元 OR 或位元 XOR。
這些運算子會保持關聯。 如果任一操作數不包含副作用,它們就是交換的。
例子:
0x0F0F -band 0xFE # int with value 0xE
0x0F0F -band 0xFEL # long with value 0xE
0x0F0F -band 14.6 # long with value 0xF
0x0F0F -bor 0xFE # int with value 0xFFF
0x0F0F -bor 0xFEL # long with value 0xFFF
0x0F0F -bor 14.40D # long with value 0xF0F
0x0F0F -bxor 0xFE # int with value 0xFF1
0x0F0F -bxor 0xFEL # long with value 0xFF1
0x0F0F -bxor 14.40D # long with value 0xF01
0x0F0F -bxor 14.6 # long with value 0xF00
7.10 邏輯運算符
語法:
logical-expression:
unary-expression -and new-lines~opt~ unary-expression
unary-expression -or new-lines~opt~ unary-expression
unary-expression -xor new-lines~opt~ unary-expression
描述:
邏輯 AND 運算子 -and(bool)視需要將操作數所指定的值轉換成 。 可能已轉換的操作數值經過邏輯 AND 運算,且類型為 bool。 如果左操作數評估為 False,則不會評估右操作數。
邏輯 OR 運算子 -or 視需要將其運算元所指定的值轉換成 bool(§6.2)。 結果是可能已轉換操作數值的邏輯 OR,且類型為 bool。 如果左操作數評估為 True,則不會評估右操作數。
邏輯 XOR 運算子 -xor 將其操作元所指定的值轉換成 bool(§6.2)。 結果是可能已轉換後的操作數值進行邏輯 XOR 的結果,且類型為 bool。
這些運算子會保持關聯。
例子:
$j = 10
$k = 20
($j -gt 5) -and (++$k -lt 15) # True -and False -> False
($j -gt 5) -and ($k -le 21) # True -and True -> True
($j++ -gt 5) -and ($j -le 10) # True -and False -> False
($j -eq 5) -and (++$k -gt 15) # False -and True -> False
$j = 10
$k = 20
($j++ -gt 5) -or (++$k -lt 15) # True -or False -> True
($j -eq 10) -or ($k -gt 15) # False -or True -> True
($j -eq 10) -or (++$k -le 20) # False -or False -> False
$j = 10
$k = 20
($j++ -gt 5) -xor (++$k -lt 15) # True -xor False -> True
($j -eq 10) -xor ($k -gt 15) # False -xor True -> True
($j -gt 10) -xor (++$k -le 25) # True -xor True -> False
7.11 指派運算符
語法:
assignment-expression:
expression assignment-operator statement
assignment-operator: *one of
= dash = += *= /= %=
描述:
指定運算子將數值儲存在 表達式所指定的可寫入位置。 如需討論 賦值運算子=,請參閱 §7.11.1。 如需所有其他指派運算子的討論,請參閱 §7.11.2。
指派表達式具有指派之後 表示式 所指定的值;不過,該指派表達式本身不會指定可寫入的位置。 如果 表示式 為類型限制 (),則該條件約束中使用的類型為結果的類型;否則,結果的類型是套用一般算術轉換之後的類型(\6.15)。
這個運算符是右結合的。
7.11.1 簡單指派
描述:
簡單指派 (=),語句所指定的值 會取代 表示式所指定之可寫入位置的值,。 不過,如果 表達式 在 Hashtable 中指定不存在的索引鍵,該索引鍵會新增至 Hashtable,且該索引鍵具有 語句所指定之值的相關值,。
如語法所示,表達式 可能指定一個用逗號分隔的可寫入位置清單。
這被稱為 多重賦值。
語句 指定一或多個逗號分隔值的清單。 任一操作數清單中的逗號是多重指派語法的一部分,而且不 不 代表二進位逗號運算符。 值取自 語句所指定的清單,、語匯順序,並儲存在 表示式所指定的對應可寫入位置。 如果 語句所指定的清單 的值比 表示式 可寫入的位置少,則多餘的位置會接受值 $null。 如果 語句所指定的清單 的值比 表達式 可寫入的位置還要多,除了最右邊的 表達式 位置外,所有其他位置都會採用對應的 語句 值,而最右邊的 表達式 位置會變成一個不受限制的一維數組,其中包含所有剩餘的 語句 值作為元素。
對於具有值的語句(§8.1.2),語句 可以是語句。
例子:
$a = 20; $b = $a + 12L # $b has type long, value 22
$hypot = [Math]::Sqrt(3*3 + 4*4) # type double, value 5
$a = $b = $c = 10.20D # all have type decimal, value 10.20
$a = (10,20,30),(1,2) # type [Object[]], Length 2
[int]$x = 10.6 # type int, value 11
[long]$x = "0xabc" # type long, value 0xabc
$a = [float] # value type literal [float]
$i,$j,$k = 10,"red",$true # $i is 10, $j is "red", $k is True
$i,$j = 10,"red",$true # $i is 10, $j is [Object[]], Length 2
$i,$j = (10,"red"),$true # $i is [Object[]], Length 2, $j is True
$i,$j,$k = 10 # $i is 10, $j is $null, $k is $null
$h = @{}
[int] $h.Lower, [int] $h.Upper = -split "10 100"
$h1 = @{ FirstName = "James"; LastName = "Anderson"; IDNum = 123 }
$h1.Dept = "Finance" # adds element Finance
$h1["City"] = "New York" # adds element City
[int]$Variable:v = 123.456 # v takes on the value 123
${E:output.txt} = "a" # write text to the given file
$Env:MyPath = "X:\data\file.txt" # define the environment variable
$Function:F = { param ($a, $b) "Hello there, $a, $b" }
F 10 "red" # define and invoke a function
function Demo { "Hi there from inside Demo" }
$Alias:A = "Demo" # create alias for function Demo
A # invoke function Demo via the alias
7.11.2 複合指派
描述:
複合指派 具有表單 E1 op= E2,而且相當於簡單指派表達式 E1 = E1 op (E2),差別在於複合指派案例中,表達式 E1 只會評估一次。 如果 表示式 受到型別限制 (§5.3),則該約束中使用的型別為結果的型別;否則,結果的型別決定於 op。 如需 *=,請參閱 §7.6.1、§7.6.2、§7.6.3;針對 /=,請參閱 §7.6.4;針對 %=,請參閱 §7.6.5;針對 +=,請參閱 §7.7.1、§7.7.2、§7.7.3;針對 -=,請參閱 §7.7.5。
注意
被指定為不受限制數值型別的操作數在結果儲存時,指派運算符可能會變更其類型。
例子:
$a = 1234; $a *= (3 + 2) # type is int, value is 1234 * (3 + 2)
$b = 10,20,30 # $b[1] has type int, value 20
$b[1] /= 6 # $b[1] has type double, value 3.33...
$i = 0
$b = 10,20,30
$b[++$i] += 2 # side effect evaluated only once
[int]$Variable:v = 10 # v takes on the value 10
$Variable:v -= 3 # 3 is subtracted from v
${E:output.txt} = "a" # write text to the given file
${E:output.txt} += "b" # append text to the file giving ab
${E:output.txt} *= 4 # replicate ab 4 times giving abababab
7.12 重新導向運算符
語法:
pipeline:
expression redirections~opt~ pipeline-tail~opt~
command verbatim-command-argument~opt~ pipeline-tail~opt~
redirections:
redirection
redirections redirection
redirection:
merging-redirection-operator
file-redirection-operator redirected-file-name
redirected-file-name:
command-argument
primary-expression
file-redirection-operator: one of
> >> 2> 2>> 3> 3>> 4> 4>>
5> 5>> 6> 6>> > >> <
merging-redirection-operator: one of
>&1 2>&1 3>&1 4>&1 5>&1 6>&1
>&2 1>&2 3>&2 4>&2 5>&2 6>&2
描述:
重新導向運算子 > 接受管線的標準輸出,並將它重新導向至 redirected-file-name所指定的位置,並覆寫該位置的目前內容。
重新導向運算子 >> 接受管線的標準輸出,並將它重新導向至 redirected-file-name所指定的位置,並附加至該位置的目前內容,如果有的話。 如果該位置不存在,則會建立它。
格式為 n> 的重新導向運算符會從管線取得數據流 n 的輸出,並將它重新導向至 重新導向檔名所指定的位置,並覆寫該位置的目前內容。
格式為 n>> 的重新導向運算符會從管線取得 n n 的數據流輸出,並將它重新導向至 redirected-file-name所指定的位置,並附加至該位置的目前內容,如果有的話。 如果該位置不存在,則會建立它。
重導運算子 m>&n 會將來自數據流 m 的輸出重定向到數據流 n所在的同一位置。
以下是有效的數據流:
| Stream | 描述 |
|---|---|
| 1 | 標準輸出數據流 |
| 2 | 錯誤輸出數據流 |
| 3 | 警告輸出流 |
| 4 | 詳細輸出流 |
| 5 | 偵錯輸出數據流 |
| * | 標準輸出、錯誤輸出、警告輸出、詳細輸出和偵錯輸出流 |
重新導向運算子 1>&2、6>、6>> 和 < 會保留供日後使用。
如果在輸出時,的 redirected-file-name 為 $null,則輸出將被捨棄。
一般而言,除非該表達式以括弧括住,否則不會將包含最上層副作用的表達式值寫入管線。 不過,如果這類表達式是作為將標準輸出重新導向的運算子的左操作數,則會寫入該值。
例子:
$i = 200 # pipeline gets nothing
$i # pipeline gets result
$i > output1.txt # result redirected to named file
++$i >> output1.txt # result appended to named file
type file1.txt 2> error1.txt # error output redirected to named file
type file2.txt 2>> error1.txt # error output appended to named file
dir -Verbose 4> verbose1.txt # verbose output redirected to named file
# Send all output to output2.txt
dir -Verbose -Debug -WarningAction Continue *> output2.txt
# error output redirected to named file, verbose output redirected
# to the same location as error output
dir -Verbose 4>&2 2> error2.txt