Gets the variables in the current console.

Syntax

  Copy Code
Get-Variable [[-Name] <string[]>] [-Exclude <string[]>] [-Include <string[]>] [-Scope <string>] [-ValueOnly] [<CommonParameters>]

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.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Include <string[]>

Specifies only the items upon which the cmdlet will act, excluding all others. Wildcards are permitted.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Name <string[]>

Specifies the name of the variable.

Required?

false

Position?

1

Default Value

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

-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.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-ValueOnly

Gets only the value of the variable.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This command supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, OutBuffer, OutVariable, WarningAction, and WarningVariable. For more information, see about_CommonParameters.

Inputs and Outputs

The input type is the type of the objects that you can pipe to the cmdlet. The return type is the type of the objects that the cmdlet returns.

Inputs

System.String

You can pipe a string that contains the variable name to Get-Variable.

Outputs

Variable object

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.

Example 1

  Copy Code
C:\PS>get-variable m*

Description
-----------
This command displays variables with names that begin with the letter "m". The value of the variables is also displayed.






Example 2

  Copy Code
C:\PS>get-variable m* -valueonly

Description
-----------
This command displays only the values of the variables with names that begin with the letter "m".






Example 3

  Copy Code
C:\PS>get-variable -include M*,P*

Description
-----------
This command gets information about the variables that begin with either the letter "M" or the letter "P".






Example 4

  Copy Code
C:\PS>get-variable -scope 0

C:\PS> compare-object (get-variable -scope 0) (get-variable -scope 1)

Description
-----------
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).






See Also