Prof. Powershell

Variables on Automatic Pilot

Check out these Automatic Variables that are defined by PowerShell with every new session.

In Windows PowerShell, we frequently create variables to store information we intend to reuse.

PS C:\> $name="Prof. PowerShell"

But PowerShell also defines a number of variables with every new session. These are referred to as Automatic Variables. Very often, these are variables used to store configuration information. Most of the time you don't need to mess with them and some of them you can't. But you can see them all with the Get-Variable cmdlet. I want to spend a little time reviewing some of the automatic variables I think you might find most useful.

PowerShell Process
The $PID variable represents the process ID of the current PowerShell session. It is possible you might have multiple PowerShell windows open and using $PID is an easy way to reference the current PowerShell session.

PS C:\> ((get-date) - (get-process -id $pid).starttime).tostring()
00:05:19.3793610

Using the process id I was able to calculate how long this PowerShell session has been running.

History Count
PowerShell maintains a command history, accessed with Get-History. The maximum number of commands retained is controlled by $MaximumHistoryCount. The default (64) is too low for my tastes so in my profile I set it to a higher number.

$MaximumHistoryCount=1000

Mail Server
If you are taking advantage of the Send-MailMessage cmdlet, you'll also want to take advantage of $PSEmailServer. Use this variable to define the default SMTP server. Once defined, you don't have to use the -SMTPServer parameter. It will default to the value of $PSEmailServer.

Where Am I?
Use the variable $pwd, to identify the current working directory. This is very handy when you want to do something with the working directory but may not know its name.

PS C:\scripts> dir $pwd | measure-object length -sum

Count    : 1178
Average  :
Sum      : 14596465
Maximum  :
Minimum  :
Property : length

Output Encoding
The $OutputEncoding varaiable controls, as the name implies, how output is encoded in your PowerShell session:

PS C:\> $OutputEncoding

IsSingleByte      : True
BodyName          : us-ascii
EncodingName      : US-ASCII
HeaderName        : us-ascii
WebName           : us-ascii
WindowsCodePage   : 1252
IsBrowserDisplay  : False
IsBrowserSave     : False
IsMailNewsDisplay : True
IsMailNewsSave    : True
EncoderFallback   : System.Text.EncoderReplacementFallback
DecoderFallback   : System.Text.DecoderReplacementFallback
IsReadOnly        : True
CodePage          : 20127

But there may be situations where you wish to change it. This is a little tricky as you need to reference the .NET class.

PS C:\> $OutputEncoding=[system.text.encoding]::Unicode

For more information on these and other variables, be sure to take a look at About_Automatic_Variables.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.