Get-Variable
取得目前控制台中的變數。
語法
Default (預設值)
Get-Variable
[[-Name] <String[]>]
[-ValueOnly]
[-Include <String[]>]
[-Exclude <String[]>]
[-Scope <String>]
[<CommonParameters>]
Description
Get-Variable指令長在目前主控台取得 PowerShell 變數。
你可以透過指定 ValueOnly 參數來取得變數的值,並且可以依名稱篩選回傳的變數。
範例
範例 1:以字母取得變數
此指令會取得以字母 m 開頭的變數名稱。 指令同時也會取得變數的值。
Get-Variable m*
範例 2:以字母取得變數值
此指令只會取得名稱以 m 開頭的變數值。
Get-Variable m* -ValueOnly
範例 3:讓變數以兩個字母排列
此指令會取得以字母 M 或字母 P 開頭的變數資訊。
Get-Variable -Include M*,P*
範例 4:依範疇取得變數
第一個指令只會取得本地作用域中定義的變數。
它等價 Get-Variable -Scope Local 於 ,且可縮寫為 gv -s 0。
第二個指令使用 Compare-Object cmdlet 尋找父範圍(Scope 1)中定義,但僅在本地範圍(Scope 0)中可見的變數。
Get-Variable -Scope 0
Compare-Object (Get-Variable -Scope 0) (Get-Variable -Scope 1)
參數
-Exclude
指定一個由此 cmdlet 排除於操作中的項目陣列。 允許使用通配符。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Include
指定指令小程式將作用的項目陣列,排除其他所有項目。 允許使用通配符。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Name
指定變數名稱。
允許使用通配符。
你也可以把變數名稱輸入 Get-Variable。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | False |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Scope
指定作用域中的變數。此參數的可接受值為:
- 全球
- Local
- 指令碼
- 相對於目前範圍的數字(0 到範圍的總數,其中0是目前的範圍,1是其父系)
本地是 預設。 如需詳細資訊,請參閱 about_Scopes。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-ValueOnly
表示此 cmdlet 只取得變數的值。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
String
Name 參數接受從管線輸入的字串,無論是從具有 Name 屬性的物件。
輸出
PSVariable
預設情況下,這個 cmdlet 會為每個變數回傳 一個 AutomationPSVariable 物件。 物件類型取決於變數。
Object
當你指定 ValueOnly 參數且該變數值為集合時,這個 cmdlet 會回傳一個 [System.Object[]]。 此行為阻止一般管線操作逐一處理變數值。 強制列舉集合的一個變通方法是將指令包在 Get-Variable 括號中。
備註
PowerShell 包含下列 Get-Variable的別名:
所有平臺:
gv
此指令本不管理環境變數。 要管理環境變數,可以使用環境提供者。