Prof. Powershell
Built-In Variables
PowerShell has several built-in variables that you might find helpful.
- By Jeffery Hicks
- 07/30/2008
PowerShell has several built-in variables that you might find helpful. Some of them you can even modify to alter your PowerShell experience. If you want to make your change permanent, then add the code to your PowerShell profile. Here are few variables you should know:
$PSHOME -- This variable displays the file path where PowerShell is installed:
PS C:\> dir $pshome
Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\System32\WindowsPowerShell\v1.0
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 8/27/2007 10:05 AM Documents
d---- 8/27/2007 10:05 AM en-US
d---- 8/27/2007 10:05 AM Examples
-a--- 8/27/2007 10:05 AM 22120 Certificate.format.ps1xml
-a--- 8/27/2007 10:05 AM 60703 DotNetTypes.format.ps1xml
-a--- 8/27/2007 10:05 AM 19730 FileSystem.format.ps1xml
-a--- 8/27/2007 10:05 AM 250197 Help.format.ps1xml
-a--- 8/27/2007 10:05 AM 340480 powershell.exe
-a--- 8/27/2007 10:05 AM 65283 PowerShellCore.format.ps1xml
-a--- 8/27/2007 10:05 AM 13394 PowerShellTrace.format.ps1xml
-a--- 8/27/2007 10:05 AM 2048 pwrshmsg.dll
-a--- 8/27/2007 10:05 AM 22528 pwrshsip.dll
-a--- 8/27/2007 10:05 AM 13540 Registry.format.ps1xml
-a--- 8/27/2007 10:05 AM 129836 types.ps1xml
This is helpful when you want to look at PowerShell's configuration files, perhaps because you want to create custom type extensions:
$MaximumHistoryCount -- This variable has a default value of 64 and it controls the maximum number of history items that PowerShell will retain. Use the Get-History cmdlet to view your history buffer. To change this variable simply assign it a new value:
PS C:\> $MaximumHistoryCount=128
$PID -- If you need to manage the PowerShell process itself, you can use $PID to return the process ID of the current PowerShell process. This may be handy if you have several PowerShell sessions open and want to kill one of them. You need to make sure you kill the right one:
PS C:\> get-process powershell | where {$_.id -ne $pid} | stop-process -confirm
$Profile -- This variable will reference your Windows PowerShell profile script that normally exists in your Documents folder. The variable is automatically populated with this value but you still need to create the script and necessary subdirectory accordingly:
PS C:\> $profile
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
If you want to use a profile you must create a script that matches $Profile. If you don't have a profile, this handy snippet will create it for you:
if (Get-ChildItem $profile -ea "silentlycontinue") {
Write-Host "Your profile already exists" -foregroundcolor Green
}
else {
#create the folder if it doesn't exist
if (! (Get-ChildItem (Split-Path $profile -parent) -ea silentlycontinue)) {
mkdir (Split-Path $profile -parent)
}
#create the file
Write-Host "creating $profile"
Set-Content $profile -value "#My PowerShell Profile"
}
dir $profile
To see these and all the other variables defined in your current PowerShell session, use the Get-Variable cmdlet.
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.