Introduction
In this first of a series of blog posts we’ll cover some basic concepts of PowerShell which are grouped under a common thread of ‘About’ in Microsoft’s documentation. For each blog post we will cover the basics of a particular About and then dive into practical, real-world ways to use the advice / concepts presented by Microsoft. Why cover this? While some of the About concepts for PowerShell have been covered by other bloggers and Microsoft as well, some of the provided Abouts are like finding a ‘diamond in the rough’. Good information hidden in plain side, rarely used and in need of some serious evangelization.
about_Automatic_Variables
Source: – about Automatic Variables
While digging for some help on a particular PowerShell concept, the About page came up in search results and while browsing this resource (like many times in the past) and ran into this intriguing nugget of information. For this blog post, we will dig into some Automatic Variables.
$$
This variable will return the last token (or part) of the last PowerShell one-liner:
$?
Displays the status of the last run cmdlet as to whether it was successful or not. Below was run right after the $$ was run in the previous example above.
$^
Will display the first token (or part) of the last PowerShell one-liner:
Notice the difference between this variable and the ‘$$’ variable’?
$Args
Store undefined parameters that can be used by functions, scripts or even script blocks. What this means is that we can pass values to these in PowerShell. For example, we can use $Args to pass values to a Function so the function can then process those values and with a larger dataset this becomes a repeatable process. Couple of simple examples of this:
Example – Pass arguments via script
Within this script we capture a couple of arguments – one will tell the script to connect to Exchange Online and the other will make sure to use MFA for the connection:
No arguments
.\Get-ExchangeOnPremAndOnlineInformation-1.19.ps1
Office 365
.\Get-ExchangeOnPremAndOnlineInformation-1.19.ps1 Office365
Office 365 and MFA
.\Get-ExchangeOnPremAndOnlineInformation-1.19.ps1 Office365 MFA
Within the script we can store these values for later use:
$Environment = $Args[0] $Authentication = $Args[1]
Example – Pass arguments to a function
In this example we pass a few
Function RemoteServerCode { $Server = $Args[0] Write-Host " " Write-Host "Analyzing event logs for server $Server" -ForegroundColor White $Log = "Application" $Logcheck = Get-WinEvent -ListLog $Log -ComputerName $Server -erroraction SilentlyContinue } RemoteServerCode $Server
Example – Pass arguments to a script block
In this block of code, we are running some code (a previously defined Script Block) remotely on some servers. In this case we are passing a computer name to the code block in the ‘-Args’ parameter:
Foreach ($Server in $Servers) { $Test = $Null $Computer = [string]$Server Invoke-Command -ComputerName $Computer -ScriptBlock $Script -Args $Computer
$ConsoleFileName
A console file (PSC1 extension) is used to load PowerShell Snap-Ins into a new PowerShell session. In an existing PowerShell session, these can be exported with the Export-Console cmdlet. This $ConsoleFileName variable will store the name of the last PSC1 file used, for reference if needed.
$EnabledExperimentalFeatures
By default, no experimental features should be enabled and this is only available in PowerShell 7.x. We can see the available Experimental Features using PowerShell:
Get-EnabledExperimentalFeatures
If none are enabled, then the variable results an empty result:
Enable-ExperimentalFeature PSCommandNotFoundSuggestion
Closing and reopening PowerShell reveals the newly enabled features:
$Error
An array variable that stores the last few error messages that occurred in the PowerShell console. In the example below, the Get-Mailbox cmdlet fails with an error. This same error (red messages) is stored in the $Error variable, see below:
The most recent error is stored as $Error[0] and the first error in the array can be revealed with $Error[-1]. This could be useful in an Automatic script, which could then log these errors to an Event Log or log file for later evaluation and troubleshooting.
Conclusion
In this blog post we covered a few of the Automatic Variables that are included in PowerShell and the intention is to cover as much of them as possible within this blog series.
———————————————————————————————————–
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: