PowerShell automatic variables are stored and maintainanced by PowerShell. This applies to Windows PowerShell that is shipped with every Windows operating system as well as PowerShell downloaded via GitHub.
In this blog post I am going to show how to compare automatic variables in Windows PowerShell and PowerShell 7.
For instance, the $IsWindows variable is missing in Windows PowerShell. It would also make no sense to check whether Windows is installed or not, because Windows PowerShell only runs on Windows OS.
$IsWindows
But there are more differences we will discover now.
Saving Varialbes in a File
First we save the list of the Windows PowerShell variables in a file.
Get-Variable | Select-Object -ExpandProperty Name | Out-File C:\Temp\windowspowershellvariables.txt
Then we save the PowerShell variables in a file.
Get-Variable | Select-Object -ExpandProperty Name | Out-File C:\Temp\powershellvariables.txt
Comparing Variables of Windows PowerShell and PowerShell
Here is the list showing us the differences:
Compare-Object -ReferenceObject (Get-Content C:\Temp\windowspowershellvariables.txt) ` -DifferenceObject (Get-Content C:\Temp\powershellvariables.txt)
To see what Windows PowerShell is missing compared to PowerShell run
(Compare-Object -ReferenceObject (Get-Content C:\Temp\windowspowershellvariables.txt) ` -DifferenceObject (Get-Content C:\Temp\powershellvariables.txt) | Where-Object {$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject) -join ", "
To see what PowerShell is missing compared to Windows PowerShell run
(Compare-Object -ReferenceObject (Get-Content C:\Temp\windowspowershellvariables.txt) ` -DifferenceObject (Get-Content C:\Temp\powershellvariables.txt) | Where-Object {$_.SideIndicator -eq "<="} | Select-Object -ExpandProperty InputObject) -join ", "
Well, task accomplished. Hope that was informative, especially for those readers who often work with automatic variables.
See you next time with PowerShell!
Categories: PowerShell