34

Is there a PowerShell command to list all previously loaded variables?

I am running some PowerShell scripts in Visual Studio and would like to list all variables that are available in the current PowerShell session.

I have tried the command:

ls variable:*;

But it returns the following:

System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
System.Management.Automation.PSVariable
5
  • 2
    When you say parameters, what exactly do you mean? All the arguments provided to a script or a function? The arguments used when launching this specific powershell session? Available variables? Commented Sep 17, 2012 at 19:44
  • ls variable:* should work (works for me at least). What host are you using which gives the bad output? Commented Sep 17, 2012 at 20:14
  • Host? I am really new to PS, what is a host? Commented Sep 17, 2012 at 20:16
  • The powershell is executed in a Visual Studio plugin called Sitecore Rocks if that is what you are after. Commented Sep 17, 2012 at 20:23
  • 3
    It looks like the host is just executing .ToString() on these PSVariable objects, which spits out the type name and not the name/value information. In the PowerShell prompt (a host) the formatting engine does the formatting of object to compose a useful string to display to the host. Looks like this host is a few cans short of a full six pack. :-) Commented Sep 17, 2012 at 20:45

3 Answers 3

56

ls variable:* should work, or Get-Variable. If these are resulting in bad output, it's due to a poorly-implemented host, not with powershell itself. If you open the standard console host (run powershell.exe), you will see that these work fine.

If you need to work around a bad host, you might have better luck dumping everything to explicit strings:

Get-Variable | Out-String

or

Get-Variable |%{ "Name : {0}`r`nValue: {1}`r`n" -f $_.Name,$_.Value }
Sign up to request clarification or add additional context in comments.

2 Comments

This worked fine, but for some reason it didn't show me the variables I expected to see. Thanks anyway.
If what you really want is the Windows environment variables, the ones you get with the "Set" command in a DOS shell, you want to use "dir env:\". PowerShell seems to have its own separate set of environment variables.
8

Interestingly, you can just type variable, and that works too!

I figured this out because I was curious as to what ls variable:* was doing. Get-Help ls tells us that it's an alias for PowerShell's Get-ChildItem, which I know will list all of the children of an Object. So, I tried just variable, and voila!

Based on this and this, it seems that what ls variable:* is doing is telling it to do some sort of scope/namespace lookup using the * (all/any) wildcard on the variable list, which, in this case, seems extraneous (ls variable:* == ls variable: == variable).

1 Comment

You should put Get-ChildItem variable:* == there at the start perhaps.
0

I frequently noodle around, testing concepts in an interactive shell, but sometimes forget my variable names or what-all I have defined.

Here is a function I wrote to wrap Get-Variable, automatically excluding any globals that were defined when the shell started up:

function Get-UserVariable()
{
    [CmdletBinding()]
    param(
        [Parameter(Position = 0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)][String[]]$Name,
        [Parameter()][Switch]$ValueOnly,
        [Parameter()][String[]]$Include,
        [Parameter()][String[]]$Exclude,
        [Parameter()][String]$Scope
    )
    $varNames = $global:PSDefaultVariables + @("PSDefaultVariables")
    $gvParams = @{'Scope' = "1"}
    if ($PSBoundParameters.ContainsKey('Name'))
    {
        $gvParams['Name'] = $Name
    }
    if ($PSBoundParameters.ContainsKey('ValueOnly'))
    {
        $gvParams['ValueOnly'] = $ValueOnly
    }
    if ($PSBoundParameters.ContainsKey('Include'))
    {
        $gvParams['Include'] = $Include
    }
    if ($PSBoundParameters.ContainsKey('Exclude'))
    {
        # This is where the magic happens, folks
        $gvParams['Exclude'] = ($Exclude + $varNames) | Sort | Get-Unique
    }
    else
    {
        $gvParams['Exclude'] = $varNames
    }
    if ($PSBoundParameters.ContainsKey('Scope'))
    {
        $gvParams['Scope'] = $Scope
    }

    gv @gvParams
<#
.SYNOPSIS
Works just like Get-Variable, but automatically excludes the names of default globals.

.DESCRIPTION
Works just like Get-Variable, but automatically excludes the names of default globals, usually captured in the user's profile.ps1 file. Insert the line:

$PSDefaultVariables = (Get-Variable).name |% { PSEscape($_) } 

...wherever you want to (either before, or after, any other stuff in profile, depending on whether you want it to be excluded by running this command.)

.PARAMETER Name
(Optional) Refer to help for Get-Variable.

.PARAMETER ValueOnly
(Optional) Refer to help for Get-Variable.

.PARAMETER Include
(Optional) Refer to help for Get-Variable.

.PARAMETER Exclude
(Optional) Refer to help for Get-Variable; any names provided here will be added to the existing list stored in $PSDefaultVariables (sorted / unique'd to eliminate duplicates.)

.PARAMETER Scope
(Optional) Refer to help for Get-Variable. The only asterisk here is that the default value is "1" just to get us out of this function's own scope, but you can override with whatever value you need. 

.OUTPUTS
Refer to help for Get-Variable.

.EXAMPLE
PS> $foo = 1,2,3
PS> Get-UserVariable

Name                           Value
----                           -----
foo                            {1, 2, 3}
#>
}
Set-Alias -Name guv -Value Get-UserVariable

Put the following line somewhere in your profile.ps1 file, either at the very top or very bottom, depending on whether you want to automatically exclude any other variables that get defined every time you run an interactive shell:

# Remember the names of all variables set up to this point (used by Get-UserVariable function)
$PSDefaultVariables = (Get-Variable).name 

Hope this is helpful!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.