Finding PowerShell Default Variables (Part 2)

In the previous tip we explained how you can use a separate new and fresh PowerShell to retrieve all default variables. When you examine these variables closely, you will discover that still some PowerShell variables are missing.

Here is a slightly modified version called Get-BuiltInPSVariable that includes all reserved PowerShell variables:

function Get-BuiltInPSVariable($Name='*')
{
  # create a new PowerShell
  $ps = [PowerShell]::Create()
  # get all variables inside of it
  $null = $ps.AddScript('$null=$host;Get-Variable') 
  $ps.Invoke() |
    Where-Object Name -like $Name
  # dispose new PowerShell
  $ps.Runspace.Close()
  $ps.Dispose()
}

To not miss out on any built-in PowerShell variable, this approach uses AddScript() instead of AddCommand(), and issues more than one command. Some PowerShell variables are created only after at least one command has run.

You can now dump all PowerShell built-in variables, or search for selected:

 
PS> Get-BuiltInPSVariable -Name *pref*

Name                           Value                                                                         
----                           -----                                                                         
ConfirmPreference              High                                                                          
DebugPreference                SilentlyContinue                                                              
ErrorActionPreference          Continue                                                                      
InformationPreference          SilentlyContinue                                                              
ProgressPreference             Continue                                                                      
VerbosePreference              SilentlyContinue                                                              
WarningPreference              Continue                                                                      
WhatIfPreference               False   
 

Twitter This Tip! ReTweet this Tip!

  • The PowerTip email says this works for all versions of PowerShell. It does not. The syntax for the Where-Object commandlet is wrong for versions 1 and 2. Change that line to:

       Where-Object {$_.Name -like $Name}

  • What does it mean to assign value to $null?

    $null = $ps.AddScript('$null=$host;Get-Variable')

  • As for...

    What does it mean to assign value to $null?

    It's, to suppress unnecessary output.

    Try it, run the code as is,  and you get this:

    Name                           Value

    ----                           -----

    $

    ?                              True

    ^

    args                           {}

    ConfirmPreference              High

    ...

    then run the code again, but first removing , the '$null =' and you get this:

    Commands            : System.Management.Automation.PSCommand

    Streams             : System.Management.Automation.PSDataStreams

    InstanceId          : 8169b4bd-ec8a-4f33-8ce2-52ea7dcc1d34

    InvocationStateInfo : System.Management.Automation.PSInvocationStateInfo

    IsNested            : False

    HadErrors           : False

    Runspace            : System.Management.Automation.Runspaces.LocalRunspace

    RunspacePool        :

    IsRunspaceOwner     : True

    HistoryString       :

    Name        : $

    Description :

    Value       :

    Visibility  : Public

    Module      :

    ModuleName  :

    Options     : None

    Attributes  : {}

  • postanote - got it, thanks