Get-Variable
Published: February 29, 2012
Updated: August 15, 2012
Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0
Get-Variable
Aliases
The following abbreviations are aliases for this cmdlet:
- gv
Syntax
Parameter Set: Default Get-Variable [[-Name] <String[]> ] [-Exclude <String[]> ] [-Include <String[]> ] [-Scope <String> ] [-ValueOnly] [ <CommonParameters>]
Detailed Description
The Get-Variable cmdlet gets the Windows PowerShell variables in the current console. You can retrieve just the values of the variables by specifying the ValueOnly parameter, and you can filter the variables returned by name.
Parameters
-Exclude<String[]>
Omits the specified items. Wildcards are permitted.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
true |
-Include<String[]>
Specifies only the items upon which the cmdlet will act, excluding all others. Wildcards are permitted.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
true |
-Name<String[]>
Specifies the name of the variable. Wildcards are permitted. You can also pipe a variable name to Get-Variable.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
1 |
|
Default Value |
none |
|
Accept Pipeline Input? |
true (ByValue, ByPropertyName) |
|
Accept Wildcard Characters? |
true |
-Scope<String>
Gets only the variables in the specified scope. Valid values are "Global", "Local", or "Script", or a number relative to the current scope (0 through the number of scopes, where 0 is the current scope and 1 is its parent). "Local" is the default. For more information, see about_Scopes.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
Local |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-ValueOnly
Gets only the value of the variable.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).
Inputs
The input type is the type of the objects that you can pipe to the cmdlet.
-
System.String
You can pipe a string that contains the variable name to Get-Variable.
Outputs
The output type is the type of the objects that the cmdlet emits.
-
System.Management.Automation.PSVariable
Get-Variable returns a System.Management.Automation variable object for each variable that it gets. The object type depends on the variable.
Notes
-
This cmdlet does not manage environment variables. To manage environment variables, you can use the environment variable provider.
Examples
-------------------------- EXAMPLE 1 --------------------------
This command gets variables with names that begin with the letter "m". The command also gets the value of the variables.
PS C:\> Get-Variable m*
-------------------------- EXAMPLE 2 --------------------------
This command gets only the values of the variables that have names that begin with "m".
PS C:\> Get-Variable m* -Valueonly
-------------------------- EXAMPLE 3 --------------------------
This command gets information about the variables that begin with either the letter "M" or the letter "P".
PS C:\> Get-Variable -Include M*,P*
-------------------------- EXAMPLE 4 --------------------------
The first command gets only the variables that are defined in the local scope. It is equivalent to "Get-Variable -Scope Local" and can be abbreviated as "gv -s 0".
The second command uses the Compare-Object cmdlet to find the variables that are defined in the parent scope (Scope 1) but are visible only in the local scope (Scope 0).
PS C:\> Get-Variable -Scope 0PS C:\>Compare-Object (Get-Variable -Scope 0) (Get-Variable -Scope 1)
Related topics