編輯附註
重要
Windows PowerShell 語言規格 3.0 於 2012 年 12 月發行,並以 Windows PowerShell 3.0 為基礎。 此規格不會反映 PowerShell 的目前狀態。 沒有計劃更新此檔以反映目前的狀態。 此文件在此提供以供歷史參考。
規格文件可作為 Microsoft Word 文件從 Microsoft 下載中心取得:https://www.microsoft.com/download/details.aspx?id=36389。該 Word 文件已轉換為在 Microsoft Learn 上展示。 在轉換期間,已進行一些編輯變更,以配合 Docs 平臺的格式設定。 已修正某些錯字和次要錯誤。
2.1 文法
此規格顯示使用兩種文法的PowerShell語言語法。 語彙文法 (•B.1) 會顯示 Unicode 字元如何結合成行終止符、批註、空格符和標記。 語法文法 (\B.2) 會示範如何結合語匯文法所產生的標記,以形成 PowerShell 腳本。
為了方便起見,這些語法的片段會在此規範的適當位置重現。
文法中字元 'a' 到 'z' 的任何用法都不區分大小寫。 這表示會忽略變數、別名、函式名稱、關鍵詞、語句和運算子中的字母大小寫。 不過,在整個規格中,這類名稱會以小寫撰寫,但某些自動和喜好設定變數除外。
2.2 語彙分析
2.2.1 腳本
語法:
提示
語法定義中的 ~opt~ 表示法指出語彙實體在語法中是選擇性的。
input:
input-elements~opt~ signature-block~opt~
input-elements:
input-element
input-elements input-element
input-element:
whitespace
comment
token
signature-block:
signature-begin signature signature-end
signature-begin:
new-line-character # SIG # Begin signature block new-line-character
signature:
base64 encoded signature blob in multiple single-line-comments
signature-end:
new-line-character # SIG # End signature block new-line-character
描述:
PowerShell 翻譯器的輸入來源資料流是腳本中的 輸入,其中包含一系列 Unicode 字元。 此數據流的語彙處理牽涉到將這些字元縮減為一連串的標記,進而成為語法分析的輸入。
腳本是儲存在 script-file的 PowerShell 命令群組。 腳本本身沒有名稱,而且會從其來源檔案中取得其名稱。 該檔案的結尾表示腳本的結尾。
文本可以選擇性地包含數字簽名。 不需要主機環境來處理簽章後面的任何文字,或看起來像簽章的任何文字。 此規格未涵蓋數位簽名的建立和使用。
2.2.2 行終止符
語法:
new-line-character:
Carriage return character (U+000D)
Line feed character (U+000A)
Carriage return character (U+000D) followed by line feed character (U+000A)
new-lines:
new-line-character
new-lines new-line-character
描述:
輸入源流中存在 新行字元,將其分割為可用於錯誤報告和偵測單行註釋結尾等的行。
行終止符可以視為空格符 (2.2.4)。
2.2.3 批注
語法:
comment:
single-line-comment
requires-comment
delimited-comment
single-line-comment:
# input-characters~opt~
input-characters:
input-character
input-characters input-character
input-character:
Any Unicode character except a new-line-character
requires-comment:
#Requires whitespace command-arguments
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
dashdash:
dash dash
delimited-comment:
< # delimited-comment-text~opt~ hashes >
delimited-comment-text:
delimited-comment-section
delimited-comment-text delimited-comment-section
delimited-comment-section:
>
hashes~opt~ not-greater-than-or-hash
hashes:
#
hashes #
not-greater-than-or-hash:
Any Unicode character except > or #
描述:
原始程式碼可以使用 批註來標註。
單行註解 以字元 # 開始,並以 新行字元結束。
分隔批註 以字元組 <# 開頭,並以字元組 #>結尾。
它可以作為來源行的一部分或整行出現,也可以跨越任意數目的來源行。
批注會被視為空格符。
上述生產專案表示
- 批注不會嵌套。
- 字元序列 <# 和 #> 在單行批注中沒有特殊意義。
- 字元 # 在分隔批注中沒有特殊意義。
語彙文法表示批註無法在標記內發生。
如需有關建立包含用於從腳本檔案生成檔案的特殊值批註之腳本檔案的資訊,請參閱第 A 節。
需要註解 指定符合的準則,以允許其包含的腳本運行。 主要準則是用來執行腳本的PowerShell版本。 最低版本需求指定如下:
#Requires -Version N[.n]
其中 N 是主要版本,而 n 是 (選擇性) 次要版本。
需要批註 可以出現在任何腳本檔案中;不過,它不能存在於函式或 Cmdlet 內。 它必須是原始碼行上的第一個項目。 腳本可以包含多個 需要附注。
只有當該序列以 # 或 <#開頭時,才會將字元序列辨識為批注。 例如,hello#there 被視為單一令牌,而 hello #there 會被視為令牌 hello,後面接著單行註解。 批註開始序列可以在空白字元之後,也可以在任何結束表達式或語句的字元(例如 )、}、]、'、"或 ;)之前加上。
需要註解 不可在嵌入式套件中出現。
有四種其他類型的 需要批注:
#Requires -Assembly AssemblyId
#Requires -Module ModuleName
#Requires -PSSnapin PSSnapin [ -Version *N* [.n] ]
#Requires -ShellId ShellId
2.2.4 空格符
語法:
whitespace:
Any character with Unicode class Zs, Zl, or Zp
Horizontal tab character (U+0009)
Vertical tab character (U+000B)
Form feed character (U+000C)
` (The backtick character U+0060) followed by new-line-character
描述:
空格符 是由一個或多個 空格符 字元的任何序列所組成。
除了空格符能夠作為詞的分隔符之外,其餘情況下會被忽略。
與某些熱門語言不同,PowerShell 不會將行終止符 () 視為空格符。 不過,行終止符若由反引號字元 `(U+0060)緊接著,則可以視為空格符。 當一行的內容在語法上是完整的時,需要這樣做,但若下一行包含預計要與前一行相關聯的符號。 例如
$number = 10 # assigns 10 to $number; nothing is written to the pipeline
+ 20 # writes 20 to the pipeline
- 50 # writes -50 to the pipeline
$number # writes $number's value, 10, to the pipeline
在此範例中,反引號表示源行仍在繼續。 下列運算式相當於 $number = 10 + 20 - 50。
$number = 10 `
+ 20 `
- 50
$number # writes $number's value to the pipeline
-20
2.3 令牌
語法:
token:
keyword
variable
command
command-parameter
command-argument-token
integer-literal
real-literal
string-literal
type-literal
operator-or-punctuator
描述:
令牌 是 PowerShell 語言中最小的語言單元。
令牌可以透過換行符、註解、空格符或其任何組合分隔。
2.3.1 關鍵詞
語法:
keyword: one of
begin break catch class
continue data define do
dynamicparam else elseif end
exit filter finally for
foreach from function if
in inlinescript parallel param
process return switch throw
trap try until using
var while workflow
描述:
關鍵詞 是一連串字元,在內容相依的位置使用時具有特殊意義。 通常,這是作為 陳述中的第一個標記;然而,如文法指出的,還有其他位置。 (類似關鍵詞但未用於關鍵詞內容的令牌,是 命令名稱 或 命令參數。)
class、define、from、using和 var 關鍵詞會保留供日後使用。
注意
編輯器的附註:PowerShell 5.0 中引進了 class 和 using 關鍵詞。 請參閱 about_Classes 與 about_Using。
2.3.2 變數
語法:
variable:
$$
$?
$^
$ variable-scope~opt~ variable-characters
@ variable-scope~opt~ variable-characters
braced-variable
braced-variable:
${ variable-scope~opt~ braced-variable-characters }
variable-scope:
Global:
Local:
Private:
Script:
Using:
Workflow:
variable-namespace
variable-namespace:
variable-characters :
variable-characters:
variable-character
variable-characters variable-character
variable-character:
A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nd
_ (The underscore character U+005F)
?
braced-variable-characters:
braced-variable-character
braced-variable-characters braced-variable-character
braced-variable-character:
Any Unicode character except
} (The closing curly brace character U+007D)
` (The backtick character U+0060)
escaped-character
escaped-character:
` (The backtick character U+0060) followed by any Unicode character
描述:
變數會在 (~5) 中詳細討論。 變數 $? 會在 §2.3.2.2中討論。 範圍會在 §3.5中討論。
$$ 和 $^ 變數會保留供互動式環境使用,但超出此規格的範圍。
命名變數名稱的方法有兩種:大括號變數名稱,開頭為 $,後面接著一組由大括號分隔的幾乎任意字元;一般變數名稱,其開頭也是 $,後面接著一組由比大括號變數名稱更嚴格限制的字元組成。 每個一般變數名稱都可以使用對應的大括弧變數名稱來表示。
$totalCost
$Maximum_Count_26
$végösszeg # Hungarian
$итог # Russian
$総計 # Japanese (Kanji)
${Maximum_Count_26}
${Name with`twhite space and `{punctuation`}}
${E:\\File.txt}
變數名稱的長度沒有限制,變數名稱中的所有字元都很重要,而且字母大小寫 不 相異。
有幾種不同類型的變數:使用者定義 (%•2.3.2.1)、自動(2.3.2.2),以及喜好設定 (\2.3.2.3)。 它們可以同時存在於相同的範圍(§3.5)。
請考慮下列函式定義和呼叫:
function Get-Power ([long]$Base, [int]$Exponent) { ... }
Get-Power 5 3 # $Base is 5, $Exponent is 3
Get-Power -Exponent 3 -Base 5 # " " "
每個參數都會依位置或名稱逐一傳遞。 不過,可以將一組參數作為一個群組傳遞,由運行時環境負責展開為個別參數。 這個自動參數展開稱為 推展。 例如
$values = 5,3 # put arguments into an array
Get-Power @values
$hash = @{ Exponent = 3; Base = 5 } # put arguments into a Hashtable
Get-Power @hash
function Get-Power2 { Get-Power @args } # arguments are in an array
Get-Power2 -Exponent 3 -Base 5 # named arguments splatted named in
@args
Get-Power2 5 3 # position arguments splatted positionally in @args
這是藉由使用 @ 而不是 $ 作為所傳遞變數的第一個字元來達成。
這個表示法只能在命令的參數中使用。
名稱會分割成不同的命名空間,每個命名空間都儲存在虛擬磁碟驅動器上()。 例如,變數會儲存在 Variable:上、環境變數儲存在 Env:、函式儲存在 Function:上,而別名則儲存在 Alias:上。 所有這些名稱都可以作為變數,通過 變數命名空間 生成的 變數範疇中訪問。 例如
function F { "Hello from F" }
$Function:F # invokes function F
Set-Alias A F
$Alias:A # invokes function F via A
$Count = 10
$Variable:Count # accesses variable Count
$Env:PATH # accesses environment variable PATH
使用帶有明確 Variable: 命名空間的變數名稱等同於使用相同變數名稱而不具備該限定性。 例如,$v 和 $Variable:v 是可互換的。
除了以語言定義,變數也可以由 Cmdlet New-Variable定義。
2.3.2.1 用戶定義的變數
文法所允許但自動或喜好設定變數未使用的任何變數名稱,都可供使用者定義變數使用。
使用者定義的變數是由使用者定義的腳本所建立和管理。
2.3.2.2 自動變數
自動變數會儲存PowerShell環境的狀態資訊。 其值可以在使用者撰寫的腳本中讀取,但無法寫入。
注意
原本在此檔中找到的數據表已移除,以減少重複。 如需自動變數的完整清單,請參閱 about_Automatic_Variables。
2.3.2.3 喜好設定變數
喜好設定變數會儲存會話的用戶喜好設定。 它們是由 PowerShell 運行時間環境建立和初始化。 其值可以用使用者撰寫的腳本來讀取和寫入。
注意
原本在此檔中找到的數據表已移除,以減少重複。 如需喜好設定變數的完整清單,請參閱 about_Preference_Variables。
2.3.3 命令
語法:
generic-token:
generic-token-parts
generic-token-parts:
generic-token-part
generic-token-parts generic-token-part
generic-token-part:
expandable-string-literal
verbatim-here-string-literal
variable
generic-token-char
generic-token-char:
Any Unicode character except
{ } ( ) ; , | & $
` (The backtick character U+0060)
double-quote-character
single-quote-character
whitespace
new-line-character
escaped-character
generic-token-with-subexpr-start:
generic-token-parts $(
2.3.4 參數
語法:
command-parameter:
dash first-parameter-char parameter-chars colon~opt~
first-parameter-char:
A Unicode character of classes Lu, Ll, Lt, Lm, or Lo
_ (The underscore character U+005F)
?
parameter-chars:
parameter-char
parameter-chars parameter-char
parameter-char:
Any Unicode character except
{ } ( ) ; , \| & . [
colon
whitespace
new-line-character
colon:
: (The colon character U+003A)
verbatim-command-argument-chars:
verbatim-command-argument-part
verbatim-command-argument-chars verbatim-command-argument-part
verbatim-command-argument-part:
verbatim-command-string
& non-ampersand-character
Any Unicode character except
|
new-line-character
non-ampersand-character:
Any Unicode character except &
verbatim-command-string:
double-quote-character non-double-quote-chars
double-quote-character
non-double-quote-chars:
non-double-quote-char
non-double-quote-chars non-double-quote-char
non-double-quote-char:
Any Unicode character except
double-quote-character
描述:
叫用命令時,資訊可能會透過一或多個 自變數傳遞給它, 其值是從命令內透過一組對應的 參數存取。 比對參數與自變數的程式稱為 參數係結。
有三種論點:
Switch 開關參數(•8.10.5) -- 這個格式為 command-parameter,其中 第一個參數字元 和 參數字元 一同構成開關名稱,對應於被呼叫命令中的參數名稱(不含其前置
-)。 如果省略尾冒號,則此自變數的存在表示對應的參數設定為$true。 如果尾端冒號存在,緊接在後面的自變數必須指定bool類型的值,而且對應的參數會設定為該值。 例如,下列調用是等效的:Set-MyProcess -Strict Set-MyProcess -Strict: $true帶引數的參數(§8.10.2)-- 這具有 command-parameter 的格式,其中 第一參數字元 和 參數字元 一起構成參數名稱,該名稱對應於被調用命令中的參數名稱(不包含開頭的 -)。 必須沒有尾端冒號。 緊接在後面的自變數會指定相關聯的值。 例如,假設命令
Get-Power,其具有參數$Base和$Exponent,下列調用是相等的:Get-Power -Base 5 -Exponent 3 Get-Power -Exponent 3 -Base 5位置參數(§8.10.2)- 自變數和其在命令中的對應參數具有位置,首個位置的索引為零。 位置 0 中的自變數係結至位置 0 中的 參數;位置 1 中的自變數係結至位置 1 中的 參數;等等。 例如,假設命令
Get-Power,其參數分別位於 0 和 1 的位置$Base和$Exponent,下列命令會叫用該命令:Get-Power 5 3
叫用命令時,可以縮寫參數名稱,只要其縮寫形式與同命令所接受的其他參數名稱不會產生混淆,就可以使用任何不同的完整名稱的前部分。
如需了解參數繫結的相關資訊,請參閱 §8.14。
2.3.5 字面值
語法:
literal:
integer-literal
real-literal
string-literal
2.3.5.1 數值字面值
數值常值有兩種:整數(§2.3.5.1.1)和實數(§2.3.5.1.2)。 兩者都可以有乘數後綴 (≦ 2.3.5.1.3)。
2.3.5.1.1 整數字面值
語法:
integer-literal:
decimal-integer-literal
hexadecimal-integer-literal
decimal-integer-literal:
decimal-digits numeric-type-suffix~opt~ numeric-multiplier~opt~
decimal-digits:
decimal-digit
decimal-digit decimal-digits
decimal-digit: one of
0 1 2 3 4 5 6 7 8 9
numeric-type-suffix:
long-type-suffix
decimal-type-suffix
hexadecimal-integer-literal:
0x hexadecimal-digits long-type-suffix~opt~
numeric-multiplier~opt~
hexadecimal-digits:
hexadecimal-digit
hexadecimal-digit decimal-digits
hexadecimal-digit: one of
0 1 2 3 4 5 6 7 8 9 a b c d e f
long-type-suffix:
l
numeric-multiplier: one of
kb mb gb tb pb
描述:
整數常值的型別取決於其值、長整數型別後綴是否存在,以及 數值乘數 的存在(§2.3.5.1.3)。
對於沒有 長整型後綴 的整數常值
- 如果其值可以透過類型 int 來表示(•4.2.3),即為其類型;
- 否則,如果其值可以以long類型來表示(§4.2.3),即為其類型。
- 否則,如果其值可以以類型 decimal (2.3.5.1.2表示,則為其類型。
- 否則,它會以 double 類型表示(§2.3.5.1.2)。
對於具有 long-type-suffix 的整數常值
- 如果其值可以用long類型表示,那麼其類型就是long(§4.2.3)。
- 否則,該常數的格式不正確。
在整數值的二進制「兩補數」表示法中,負值的數量比正數多一個。 針對 int 類型,該額外值為 -2147483648。 針對 long 類型,這個額外值為 -9223372036854775808。 即使標記2147483648通常會被視為long類型的常值,但如果它緊接在一元運算符號“-”之後,該運算符號和常值會被視為int類型的常值,且具有最小值。 同樣地,即使標記9223372036854775808通常會被視為 decimal 類型的實值常數,但如果它緊接在一元負號運算符之前,那麼該運算符和常數會被視為 long 類型的常數,且具有最小值。
整數常值的某些範例為 123 (int)、123L(long)和 200000000000(long)。
不存在 byte 類型的整數字面值。
2.3.5.1.2 實數文字
語法:
real-literal:
decimal-digits . decimal-digits exponent-part~opt~ decimal-type-suffix~opt~ numeric-multiplier~opt~
. decimal-digits exponent-part~opt~ decimal-type-suffix~opt~ numeric-multiplier~opt~
decimal-digits exponent-part decimal-type-suffix~opt~ numeric-multiplier~opt~
exponent-part:
e sign~opt~ decimal-digits
sign: one of
+
dash
decimal-type-suffix:
d
l
numeric-multiplier: one of
kb mb gb tb pb
dash:
- (U+002D)
EnDash character (U+2013)
EmDash character (U+2014)
Horizontal bar character (U+2015)
描述:
實際常值可能包含 數值乘數 (§2.3.5.1.3)。
實際常值有兩種:雙 和 十進位。 這些會分別以 十進位類型後綴的不存在或存在來表示。 (沒有像 浮動實值這樣的事情。
雙實常值的類型為 double (•4.2.4.1)。 十進制實數文字的類型為小數(§4.2.4.2)。 小數實數字面值中分數部分的尾端零是具有意義的。
如果雙精度浮點數常值中 指數部分的的 十進位數 小於支援的最小值,則該雙精度浮點數常值的值為 0。 如果在十進位實數常值中,指數部分的值的 十進位數 小於支援的最小值,則該常值格式不正確。 如果雙精度或十進位制實數字面常數中的 指數部分的 十進位數字 大於支援的最大值,則該字面常數的格式不正確。
雙實值的某些範例包括 1.、1.23、.45e35、32.e+12 和 123.456E-231。
十進制實值的某些範例為 1d(其小數位數為 0)、1.20d(具有小數位數 2)、1.23450e1d(亦即 12.3450,其小數位數為 4)、1.2345e3d (亦即 1234.5,其縮放比例為 1)、1.2345e-1d(即 0.12345,具有 5 縮放比例),以及 1.2345e-3d(亦即 0.0012345,其縮放比例為 7)。
注意
因為雙精度浮點數常值不一定要有分數或指數部分,所以需要在 (123).M 中使用分組括弧,以確保針對值為 123 的整數對象選擇屬性或方法 M。 如果沒有這些括弧,字面常量將無法正確生成。
注意
雖然 PowerShell 並不提供無限大及 NaN 的常數語法,但可以透過類型 float 和 double 的靜態唯讀屬性 PositiveInfinity、NegativeInfinity 與 NaN 來取得類似常數的對應值(§4.2.4.1)。
文法允許以雙實常值開頭的內容具有 l 或 L 類型後綴。 這類符號實際上是整數常值,其值由 long 類型表示。
注意
此功能已保留,以確保與舊版 PowerShell 的向後相容性。 不過,程式設計人員不建議使用此形式的整數常值,因為它們很容易遮蔽常值的實際值。 例如,1.2L 具有值 1、1.2345e1L 具有值 12,而 1.2345e-5L 的值為 0,這一點並不明顯。
2.3.5.1.3 乘數後綴
語法:
numeric-multiplier: *one of*
kb mb gb tb pb
描述:
為了方便起見,整數和實際常值可以包含 數值乘數,這表示一組常用的 10 乘冪之一。 數值乘數 可以用大寫或小寫字母的任何組合來撰寫。
| 乘數 | 意義 | 範例 |
|---|---|---|
| kb | 千位元組(KB,1024 位元組) | 1kb ≡ 1024 |
| mb | 兆字節(1024 x 1024) | 1.30Dmb ≡ 1363148.80 |
| GB | GB (1024 x 1024 x 1024) | 0x10Gb ≡ 17179869184 |
| 結核病 | 兆字節 (1024 x 1024 x 1024 x 1024) | 1.4e23tb ≡ 1.5393162788864E+35 |
| 鉛 | pbyte (1024 x 1024 x 1024 x 1024 x 1024) | 0x12Lpb ≡ 20266198323167232 |
2.3.5.2 字串常值
語法:
string-literal:
expandable-string-literal
expandable-here-string-literal
verbatim-string-literal
verbatim-here-string-literal
expandable-string-literal:
double-quote-character expandable-string-characters~opt~ dollars~opt~ double-quote-character
double-quote-character:
" (U+0022)
Left double quotation mark (U+201C)
Right double quotation mark (U+201D)
Double low-9 quotation mark (U+201E)
expandable-string-characters:
expandable-string-part
expandable-string-characters
expandable-string-part
expandable-string-part:
Any Unicode character except
$
double-quote-character
` (The backtick character U+0060)
braced-variable
$ Any Unicode character except
(
{
double-quote-character
` (The backtick character U+0060)*
$ escaped-character
escaped-character
double-quote-character double-quote-character
dollars:
$
dollars $
expandable-here-string-literal:
@ double-quote-character whitespace~opt~ new-line-character
expandable-here-string-characters~opt~ new-line-character double-quote-character @
expandable-here-string-characters:
expandable-here-string-part
expandable-here-string-characters expandable-here-string-part
expandable-here-string-part:
Any Unicode character except
$
new-line-character
braced-variable
$ Any Unicode character except
(
new-line-character
$ new-line-character Any Unicode character except double-quote-char
$ new-line-character double-quote-char Any Unicode character except @
new-line-character Any Unicode character except double-quote-char
new-line-character double-quote-char Any Unicode character except @
expandable-string-with-subexpr-start:
double-quote-character expandable-string-chars~opt~ $(
expandable-string-with-subexpr-end:
double-quote-char
expandable-here-string-with-subexpr-start:
@ double-quote-character whitespace~opt~ new-line-character expandable-here-string-chars~opt~ $(
expandable-here-string-with-subexpr-end:
new-line-character double-quote-character @
verbatim-string-literal:
single-quote-character verbatim-string-characters~opt~ single-quote-char
single-quote-character:
' (U+0027)
Left single quotation mark (U+2018)
Right single quotation mark (U+2019)
Single low-9 quotation mark (U+201A)
Single high-reversed-9 quotation mark (U+201B)
verbatim-string-characters:
verbatim-string-part
verbatim-string-characters verbatim-string-part
verbatim-string-part:
*Any Unicode character except* single-quote-character
single-quote-character single-quote-character
verbatim-here-string-literal:
@ single-quote-character whitespace~opt~ new-line-character
verbatim-here-string-characters~opt~ new-line-character
single-quote-character *@*
verbatim-*here-string-characters:
verbatim-here-string-part
verbatim-here-string-characters verbatim-here-string-part
verbatim-here-string-part:
Any Unicode character except* new-line-character
new-line-character Any Unicode character except single-quote-character
new-line-character single-quote-character Any Unicode character except @
描述:
字串字面值有四種:
逐字串常值(單行且以單引號包圍),這是一個由零個或多個字元組成的序列,以一對單引號字元包圍。 範例包括 '' 和 'red'。
可展開字串常值(單行雙引號),這是一個由一對 雙引號分隔的零或多個字元序列。 範例包括「」和「紅色」。
逐字字串常值(多行單引號),這是以字元組 @單引號字元 和 單引號字元@分隔的零个或多个字符序列,分別包含在兩個或多個來源行上。 範例包括:
@' '@ @' line 1 '@ @' line 1 line 2 '@expandable-here-string-literal(多行雙引號字串),這是一個以字符組 @雙引號字符 和 雙引號字符@ 分隔的零或多個字符序列,分佈在兩行或多行的原始碼中。 範例包括:
@" "@ @" line 1 "@ @" line 1 line 2 "@
對於 verbatim-here-string-literal和 expandable-here-string-literal,除了空格(可以忽略)之外,任何字元都不能跟在開頭分隔符號組之後的同一來源行上,並且任何字元也不能出現在結尾分隔符號組之前的同一來源行上。
本文逐字字串常值 或 可展開字串常值 的內容會在開頭分隔符後的來源行開頭開始,並在結尾分隔符前的來源行結尾結束。 內容可能是空的。 在結尾分隔符之前的最後一行源代码中的行終止符不是該文字值的内容的一部分。
任何此類字面量的類型為字串(§4.3.1)。
用來分隔 逐字字串常值 或 可擴充字串常值 的字元可以透過將該字元連續寫兩次來包含在這類字串常值中。 例如,'What''s the time?' 和 "I said, ""Hello""."。 不過,單引號字元 在 可展開字串常值內沒有特殊意義,雙引號字元 在 逐字字串常值內沒有特殊意義。
expandable-string-literal 和 expandable-here-string-literal 可能包含 逸出字元 (§2.3.7)。 例如,將下列字串常值寫入管線時,結果如下所示:
"column1`tcolumn2`nsecond line, `"Hello`", ```Q`5`!"
column1<horizontal-tab>column2<new-line>
second line, "Hello", `Q5!
如果 expandable-string-literal 或 expandable-here-string-literal 包含變數的名稱,除非該名稱前面緊接著逸出字元,否則會由該變數值的字串表示法取代(§6.7)。 這稱為 變數替代。
注意
如果變數名稱是某些較大型表達式的一部分,則只會取代變數名稱。 例如,如果 $a 是包含元素 100 和 200 的陣列,">$a.Length<" 會產生 >100 200.Length<,而 ">$($a.Length)<" 則會產生 >2<。 請參閱下方的子表達式展開。
例如,原始程式碼
$count = 10
"The value of `$count is $count"
會生成expandable-string-literal
The value of $count is 10.
請考慮下列事項:
$a = "red","blue"
"`$a[0] is $a[0], `$a[0] is $($a[0])" # second [0] is taken literally
結果為
$a[0] is red blue[0], $a[0] is red
expandable-string-literals 和 expandable-here-string-literal也支援一種稱為 子表達式展開的替代,方法是將某種格式的文字 $( ... ) 視為 子表達式(§7.1.6)。 這類文字會以該表達式值的字串表示法(§6.8)來取代。 任何在 子運算式的 語句清單 中用來分隔字符的空白,在考慮結果字串建構時會被忽略。
範例
$count = 10
"$count + 5 is $($count + 5)"
"$count + 5 is `$($count + 5)"
"$count + 5 is `$(`$count + 5)"
會產生下列 expandable-string-literals:
10 + 5 is 15
10 + 5 is $(10 + 5)
10 + 5 is $($count + 5)
下列來源:
$i = 5; $j = 10; $k = 15
"`$i, `$j, and `$k have the values $( $i; $j; $k )"
會產生下列 expandable-string-literal:
$i, $j, and $k have the values 5 10 15
這四行可以更簡潔地撰寫,如下所示:
"`$i, `$j, and `$k have the values $(($i = 5); ($j = 10); ($k = 15))"
在下列範例中,
"First 10 squares: $(for ($i = 1; $i -le 10; ++$i) { "$i $($i*$i) " })"
產生的 可擴充字串文字 如下所示:
First 10 squares: 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100
如所示,子表達式 可以包含同時具備變數替代和子表達式展開的字串文字。 另請注意,內部 expandable-string-literal的分隔符不需要逸出;它們位於 子表達式內的事實 表示它們不能是外部 expandable-string-literal的終止符。
expandable-string-literal 或 expandable-here-string-literal 包含變數替換或子表達式展開時,每次使用該常值都會進行評估;例如
$a = 10
$s1 = "`$a = $($a; ++$a)"
"`$s1 = >$s1<"
$s2 = "`$a = $($a; ++$a)"
"`$s2 = >$s2<"
$s2 = $s1
"`$s2 = >$s2<"
這會產生下列 expandable-string-literal:
$s1 = >$a = 10<
$s2 = >$a = 11<
$s2 = >$a = 10<
逐字字串字面值 的內容會逐字保留,包括內容內的任何前導或尾隨空白。 因此,內嵌的 單引號字元無需重複,並且不進行替代或展開。 例如
$lit = @'
That's it!
2 * 3 = $(2*3)
'@
這會導致字面值
That's it!
2 * 3 = $(2*3)
expandable-here-string-literal 的內容適用於替代和展開,但在本文中,任何在 子表達式之外的前置或尾端空格符都會被逐字取用,而內嵌的 雙引號字元不須重複。 例如
$lit = @"
That's it!
2 * 3 = $(2*3)
"@
這會在展開時產生如下的字面值:
That's it!
2 * 3 = 6
對於 逐字字串文字常值和 擴展的字串文本常值,本體中的每一個行終止符都會根據實作決定的方式呈現出現的常值。 例如,在
$lit = @"
abc
xyz
"@
第二行有兩個前導空格,而內容的第一行和第二行都有行終止符;然而,第二行的行終止符 不是內容的一部分。 產生的常值相當於:"abc<implementation-defined character sequence>xyz"。
注意
為了提升程式碼的可讀性,長字串常值可以跨多行程式碼中斷,而不插入行終止符。 這是透過將每個部分撰寫為獨立的常值,然後使用 + 運算符將這些部分串接起來(§7.7.2)。 這個運算子允許其操作數指定四種字串文字常數中的任何一種。
注意
雖然沒有字元字面值本身之類的功能,但存取 1 個字元字串中的第一個字元即可達到相同的效果,如下所示:[char]"A" 或 "A"[0]。
對於 逐字字串常值和 可擴展字串常值,每個字串中的行終止符都完全相符地表示。
2.3.5.3 Null 字面值
請參閱自動變數 $null (2.3.2.2)。
2.3.5.4 布爾值常值
請參閱自動變數 $false 和 $true (!2.3.2.2)。
2.3.5.5 陣列字面值
PowerShell 允許使用一元逗號運算符(§7.2.1)、陣列表達式(§7.1.7)、二元逗號運算符(§7.3)和範圍運算符(§7.4)。
2.3.5.6 哈希字面值
PowerShell 允許使用 哈希常值表示式 來撰寫哈希表類型的表達式(\7.1.9)
2.3.5.7 類型名稱
語法:
type-name:
type-identifier
type-name . type-identifier
type-identifier:
type-characters
type-characters:
type-character
type-characters type-character
type-character:
A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nd
_ (The underscore character U+005F)
array-type-name:
type-name [
generic-type-name:
type-name [
2.3.6 運算符和標點符號
語法:
operator-or-punctuator: one of
{ } [ ] ( ) @( @{ $( ;
&& || & | , ++ .. :: .
! * / % + - --
-and -band -bnot -bor
-bxor -not -or -xor
assignment-operator
merging-redirection-operator
file-redirection-operator
comparison-operator
format-operator
assignment-operator: one of
= -= += *= /= %=
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
comparison-operator: *one of
-as -ccontains -ceq
-cge -cgt -cle
-clike -clt -cmatch
-cne -cnotcontains -cnotlike
-cnotmatch -contains -creplace
-csplit -eq -ge
-gt -icontains -ieq
-ige -igt -ile
-ilike -ilt -imatch
-in -ine -inotcontains
-inotlike -inotmatch -ireplace
-is -isnot -isplit
-join -le -like
-lt -match -ne
-notcontains -notin -notlike
-notmatch -replace -shl*
-shr -split
format-operator:
-f
描述:
&& 和 || 保留供日後使用。
注意
編輯器的附注:管線鏈結運算子 && 和 || 是在 PowerShell 7 中引進的。 參見 about_Pipeline_Chain_Operators。
運算子中下列 虛線 的名稱只保留於運算符內容中。
以 破折號 開頭的運算符,其 破折號 與後面的標記之間不得有任何空格。
2.3.7 逸出字元
語法:
escaped-character:
` (The backtick character U+0060) followed by any Unicode character
描述:
逸出字元 是一種透過為字元添加前綴 Backtick 符號(U+0060)來賦予其特殊解釋的方法。 下表顯示每個 逸出字元的意義:
| 跳脫字元 | 意義 |
|---|---|
`a |
警示 (U+0007) |
`b |
退格鍵(U+0008) |
`f |
表單摘要 (U+000C) |
`n |
新線 (U+000A) |
`r |
回車符 (U+000D) |
`t |
水平索引標籤 (U+0009) |
`v |
垂直索引標籤 (U+0009) |
`' |
單引號 (U+0027) |
`" |
雙引號(U+0022) |
`` |
反引號 (U+0060) |
`0 |
NUL (U+0000) |
`x |
如果 x 是上述字元以外的字元,則會忽略反引號字元,而 x 會被視為字面意思。 |
上述表格中最後一個項目的意涵是,原本用來分隔字元的空格可以改為組成字元的一部分。 例如,包含空格的檔名可以寫入為 Test` Data.txt(以及 'Test Data.txt' 或 "Test Data.txt")。