Get-Variable
Use Get-Variable to list the variables in the current scope, or retrieve one by name.
Documentation: Microsoft Learn — Get-Variable
Syntax
See the command anatomy
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Get-Variable [[-Name] <string[]>] [-ValueOnly] [-Include <String[]>] [-Exclude <String[]>] [-Scope <String>] [<CommonParameters>]- Stage 1Get-Variable
-Name -ValueOnly -Include -Exclude -Scope
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Follow the objects
Run the command to inspect each stage.
Examples
Get-Variable -Name PSVersionTable
Show reference output
Sample output · fictional data, shaped like the real thing
Name Value ---- ----- PSVersion 7.5.2 PSEdition Core GitCommitId 7.5.2 OS Microsoft Windows 11 Pro
Select first 5
Show reference output
Sample output · fictional data, shaped like the real thing
Name Value
---- -----
ConfirmPreference High
DebugPreference SilentlyContinue
ErrorActionPreference Continue
HOME C:\Users\demo
PSVersionTable {PSVersion, PSEdition, OS...}Get-Variable -Name PS*
Show reference output
Sample output · fictional data, shaped like the real thing
Name ---- PSCulture PSEdition PSHOME PSVersionTable PWD
Parameters
Parameter definitions:
-Name-ValueOnly-Include-Exclude-Scope
Practical use and troubleshooting
Practical scenario: Use Get-Variable when you need to list the variables in the current scope, or retrieve one by name. Review syntax and sample output, then try an example in the browser lab. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.
Common error and fix: An unexpected value can come from querying the wrong scope. Inspect the variable with an explicit scope before treating a similarly named value as the one created by the current function. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.
See also: Remove-Variable. Remove-Variable removes a variable binding; Get-Variable inspects it. Removing a name is different from assigning an empty string or null to its value. Read the linked command before substituting it; similar names do not imply identical inputs or output.
Recap
Get-Variable Use Get-Variable to list the variables in the current scope, or retrieve one by name.
Next: Remove-Variable