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!
ls variable:*should work (works for me at least). What host are you using which gives the bad output?.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. :-)