How to Create a PowerShell Profile
Here at Idea 11 we're big fans of PowerShell. We use it every day to support our customers and develop scripts and tools to help with our services and support.
When you spend as much time in PowerShell as we do it is useful to create a PowerShell profile. A PowerShell profile is simply a PowerShell script that automatically runs when you open a new PowerShell window. PowerShell profiles are usually used to configure your PowerShell environment to fit your needs, or to load custom functions that you can then use during your PowerShell administration tasks.
There are six PowerShell profiles that relate to the PowerShell console (the command line interface) and the PowerShell ISE (the integrated scripting environment). To see the four console-related profiles run this command:
PS C:\> $profile | Get-Member -MemberType NoteProperty | fl name,definition Name : AllUsersAllHosts Definition : System.String AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 Name : AllUsersCurrentHost Definition : System.String AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 Name : CurrentUserAllHosts Definition : System.String CurrentUserAllHosts=C:\Users\Paul\Documents\WindowsPowerShell\profile.ps1 Name : CurrentUserCurrentHost Definition : System.String CurrentUserCurrentHost=C:\Users\Paul\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
For this article let's just focus on the "Current User, Current Host" profile.
First, is there an existing profile? Use Test-Path to check this. The $profile variable stores the path to the profile file itself:
PS C:\> $profile C:\Users\Paul\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
So we can simply run:
PS C:\> Test-Path $profile True
If the result is False you can create the profile file using New-Item.
PS C:\> New-Item $profile -ItemType File -Force Directory: C:\Users\Paul\Documents\WindowsPowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 3/12/2014 2:30 PM 0 Microsoft.PowerShell_profile.ps1
After the profile file has been created you can edit it using the PowerShell ISE.
PS C:\> powershell_ise.exe $profile
From here you can add any PowerShell functions or commands that you want to run automatically each time you open a new PowerShell window. As a simple example here is how I configure my PowerShell windows to switch to the C:\Scripts folder instead of the default C:\Users\Paul folder, so that I am already in the location where my script files are stored.
#Change directories to C:\Scripts folder If (Test-Path C:\Scripts) { Set-Location C:\Scripts } else { Write-Warning "C:\Scripts folder not found on this computer." }
Your PowerShell profile will grow and change over time to suit your own purposes, so why not create one today so you can start adding items to it.