Introduction
Last month, actually exactly a month ago, I published the first part of this series on About About’s and in this post I want to continue on covering some of the variables that were found I about_Automatic_Variables. Like the last post I will break down as many of these variables and as there are so many to be found, future articles will be coming out in the future. Now, on to more variables.
$ExecutionContext
Contains contextual information about the current shell.
If we dig into the variable we can pull some useful information. Below is a chain of discovery from the base $ExecutionContext to the end result of $ExecutionContext.Host.UI.RawUI
We can follow the same to pull the current path information for the session:
$False
This variable is a condition variable where we can use for comparisons, setting the value of another variable, setting a property on an object or answer a question. Below are some sample uses:
Comparison:
If ($Test -eq $False) {Write-Host "Test variable is set to False"}
Set a variable to False:
$Found = $False
Setting a property to False
Set-Mailbox Damian -LitigationHoldEnabled $False
Answer Question:
Update-Module MicrosoftTeams -RequiredVersion $NewestVersion -Confirm:$False
$Foreach
Contains the current enumerator being processed in the Foreach loop. For example if we have a list of names stored in a $Names variable like so:
$Names = "Damian","Dave","Jaap","Michael"
And then we loop all these names with a Foreach loop, each iteration of the loop, the $Foreach variable contains the current $Name value. Example:
Foreach ($Name in $Names) { $Foreach.Current }
$Home
This variable contains the home directory for the logged in user. Sample:
$Home
$Host
Contains the current configuration for the host application for PowerShell. Let’s break down the variable to see what we can learn about it.
$Host
Notice that there are three properties that do not display information, but refer to other potential information – UI, PrivateData and RunSpace. Let’s break these down:
UI
What we notice about this one, is that there is a sub value called RawUI, which we can break down at the same time:
All of these settings deal with attributes of the current PowerShell window itself. We can potentially change settings for a current session if we desire, for example, changing buffer sizes to handle the display of larger data sets:
$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 9999)
$Host.PrivateData
With this sub value, we see the color scheme for various system generated messages in PowerShell:
‘Error’ defines the color for error messages in the shell, Warning for any warning messages and so on. I would not recommend that these be changed as these are universal colors that those using PowerShell are quite used to by now.
$Host.Runspace
If we change the formatting to a list style, we can see all the values that we are missing:
$Host.Runspace | Fl
Now, we have even more values to expand – Events, Job Manager, Debugger, IntialSessionState and SessionStateProxy. A quick summary of these expanded values:
If you want to explore some more values or properties contained in this variable, dig into these with your free time:
Example 1
Example 2
Interesting link – https://jeffbrown.tech/how-to-create-prompts-in-powershell/
$Input
Enumerates input passed to it.
Example from help and explanation:
echo Hello | pwsh -Command """$input World!"""
How does this work? The pipe feeds the input from the left to the $Input variable on the right side of the pipe and we get this result:
Another usage is to pass data to Functions
Function MailboxCheck { Process { Try { Get-ExOMailbox -Identity $Input -ErrorAction STOP | Ft Name,RecipientType } Catch { Write-Host 'Mailbox not found.' } } }
I can then pass two names, or data to the function which $Input will then be populated for the function. One caveat is that it can only be used once when the $Input variable is then emptied and $Null.
"Damian","Dave" | MailboxCheck
$IsCoreCLR
Is the current PowerShell session running on .NET Core Runtime. True if it is and False if it was not. Results from a Windows 11 computer:
We can also see what version we are on via $PsVersionTable – see below.
$IsLinux
Is the current PowerShell session run on a Linux OS computer. True if it is and False if it was not. Results from a Windows 11 computer:
$IsMacOS
Is the current PowerShell session run on a Mac OS computer. True if it is and False if it was not. Results from a Windows 11 computer:
$IsWindows
Is the current PowerShell session run on a Windows OS computer. True if it is and False if it was not. Results from a Windows 11 computer:
Conclusion
In this blog post we covered a few more of the Automatic Variables that are included in PowerShell. Looks for more parts in the near future.
———————————————————————————————————–
Comments? Questions?
Feel free to leave your Comments below! Learn to more efficiently utilize PowerShell to manage Exchange Server, Exchange Online, Microsoft Defender for Office or Microsoft Purview Compliance portals by picking up frequently updated eBooks: