PowerShell Genie

Get-Variable

Use Get-Variable to list the variables in the current scope, or retrieve one by name.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Get-Variable

Syntax

1PS> Get-Variable [[-Name] <string[]>] [-ValueOnly] [-Include <String[]>] [-Exclude <String[]>] [-Scope <String>] [<CommonParameters>]
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>]
  1. Stage 1Get-Variable-Name -ValueOnly -Include -Exclude -Scope

    Try it

    Windows PowerShell

    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.

    Interactive simulation · fictional data

    Examples

    Get-Variable -Name PSVersionTable

    1PS> Get-Variable -Name PSVersionTable -ValueOnly
    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

    1PS> Get-Variable | Select-Object -First 5 Name, Value
    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*

    1PS> Get-Variable -Name PS* | Select-Object Name
    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

    Get-Variable on Microsoft Learn

    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.

    Get-Variable reference on Microsoft Learn

    Recap

    Get-Variable Use Get-Variable to list the variables in the current scope, or retrieve one by name.

    Next: Remove-Variable
    Genie guideGet-Variable

    Use Get-Variable to list the variables in the current scope, or retrieve one by name. The console below is pre-loaded — run it, then change a value and run it again.