Get Variable - PowerShell
Overview
Get-Variable is a PowerShell command used to retrieve variables from the current scope, a specific scope, or a provider. It provides detailed information about each variable, including its name, value, type, and other properties.
Syntax
Get-Variable [-Name] <string[]> [-Value] <object[]> [-Force] [-Include] <string[]> [-Exclude] <string[]> [-OutVariable] <string> [-ErrorAction] <ActionPreference> [-ErrorVariable] <string>
Options/Flags
-
-Name (alias
-n
): Filter the results to include only variables with the specified names. Accepts wildcard characters (* and ?). -
-Value (alias
-v
): Filter the results to include only variables with the specified values. Supports equality checks and wildcard matching. -
-Force (alias
-f
): Forces the command to return all variables, even if they are hidden. -
-Include (alias
-inc
): Includes variables with the specified names or patterns. Takes precedence over-Exclude
. -
-Exclude (alias
-exc
): Excludes variables with the specified names or patterns. -
-OutVariable (alias
-ov
): Stores the results in the specified variable. -
-ErrorAction (alias
-ea
): Specifies the action to take when an error occurs. -
-ErrorVariable (alias
-ev
): Stores the error message in the specified variable.
Examples
Example 1: Getting all variables:
Get-Variable
Example 2: Filtering variables by name:
Get-Variable -Name "MyVar*"
Example 3: Getting variables with specific values:
Get-Variable -Value 100
Example 4: Excluding hidden variables:
Get-Variable -Force -Exclude Hidden:*
Common Issues
-
Variable not found: Ensure the variable name is spelled correctly and exists in the current scope.
-
Access denied: If you are trying to access variables from a different scope, ensure you have sufficient permissions.
Integration
Get-Variable can be used in combination with other commands, such as:
- Set-Variable: To modify variable values.
- New-Variable: To create new variables.
- Remove-Variable: To delete variables.
Related Commands
- Set-Variable
- New-Variable
- Remove-Variable
- Invoke-Variable