Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
November 4, 2025

Get My Things

In this issue:

  • PowerShell Defaults
  • Variables
  • Aliases
  • PSDrives
  • Summary

I run my life from a PowerShell prompt. I have written modules, custom functions, scripts and more to make my life easier. I also love aliases and variables to speed up my workflow and save typing. The problem is that I have so many customizations, it is impossible to remember them all. I'll know I have a custom command to do something, but I can't remember what I called it, or what shortcut I created for it.

This is why PowerShell has commands like Get-Variable and Get-Command. However my challenge is to filter out the defaults. What variables are defined in my session that I created? What aliases have I defined? What commands are available to me that I created or installed? How do I get my things?

PowerShell Defaults

Conceptually, I need to compare default things with the current list of things, and filter out the defaults. I could always run:

PS C:\> $defVariable = pwsh -NoLogo -NoProfile -command "&{Get-Variable}"
PS C:\> $defVariable | Select -first 10

Name                           Value
----                           -----
?                              True
^
$
args                           {}
ConfirmPreference              High
DebugPreference                SilentlyContinue
EnabledExperimentalFeatures    {}

The $defVariable variable is a string. Instead of trying to parse it, I could simply return the variable names:

$defVariable = pwsh -NoLogo -NoProfile -command "&{(Get-Variable).Name}"

Then I can compare the current variable names to the default variable names:

PS C:\> Get-Variable | where {$defVariable -notContains $_.Name} | Select -first 5

Name                           Value
----                           -----
defVariable                    {?, ^, $, args…}
DriveData                      "Name","Root","Description"…
in                             {@{Name=adsvw; Definition=C:\scripts\exes\ADSVW.exe; Description=…
jsonFile                       C:\scripts\PROSPERO-aliases.json
LASTEXITCODE                   0

Another option is to use the InitialSessionState property of the current host's runspace. This property contains the default commands, variables, aliases, and more for a PowerShell session. I can use this to get the default variable names:

InitialSessionState
figure 1

The object output is a little different, but nothing you can't handle.

PS C:\> $host.Runspace.InitialSessionState.Variables | Select Name,Value -first 5

Name           Value
----           -----
$
^
StackTrace
PSStyle        System.Management.Automation.PSStyle
OutputEncoding System.Text.UTF8Encoding+UTF8EncodingSealed

However, it appears this data isn't sufficient to define "default".

The better approach seems to be creating a new PowerShell instance.

$ps = [powershell]::Create()

I can add a command to this new instance to get the default variables:

[void]$ps.commands.AddCommand("Get-Variable")

Then invoke it.

$var = $ps.Invoke()

This should be the initial set of variables.

PS C:\> $var | select -first 10

Name                           Value
----                           -----
?                              True
^
$
ConfirmPreference              High
DebugPreference                SilentlyContinue
EnabledExperimentalFeatures    {}
Error                          {}
ErrorActionPreference          Continue
ErrorView                      ConciseView
ExecutionContext               System.Management.Automation.EngineIntrinsics

When you are finished with this, you should dispose of it.

$ps.Dispose()

Let's dive into how to use this to get my things.

Variables

Since I've already started on variables, let's continue. I can get an initial list of default variables from a new PowerShell instance:

$ps = [powershell]::Create()
[void]$ps.commands.AddCommand("Get-Variable")
$out = $ps.invoke()
$PSVariables = $out.name
$ps.Dispose()
Want to read the full issue?
GitHub Bluesky LinkedIn Mastodon https://jdhitsoluti…
Powered by Buttondown, the easiest way to start and grow your newsletter.