Getty Images
How to find and customize your PowerShell profile
PowerShell is integral for many admins, and any way to streamline how they use the scripting tool is key. A PowerShell profile is one way to tailor a more streamlined approach.
If you run the same commands every time you launch the PowerShell console, then it's probably time to customize your PowerShell profile for a smoother scripting experience.
When you develop a profile in PowerShell, you define your settings as a PowerShell script to make the process easier. In this article, learn about the PowerShell profile, how to edit your profile for any PowerShell console -- the newer cross-platform PowerShell 7, Windows PowerShell 5.1, Visual Studio (VS) Code and PowerShell Integrated Script Environment (ISE) -- and several handy additions for your profile.
What is the PowerShell profile?
The PowerShell profile is a PowerShell script that runs every time you launch PowerShell, except when you use the -NoProfile flag. The PowerShell profile location varies depending on several conditions:
- The version of PowerShell: Windows PowerShell or PowerShell 7?
- Is the profile for all users or the current user?
- Is the profile host application-specific?
The following lists are directories. The names of the profile scripts depend on the host application.
For all users, the profile is located at the following locations:
- Windows PowerShell: $PSHOME\
- PowerShell 7:
- Windows: $PSHOME\
- Linux: /usr/local/Microsoft/powershell/7/
- macOS: /usr/local/Microsoft/powershell/7/
For specific users, the profile is located here:
- Windows PowerShell: $HOME\Documents\WindowsPowerShell\
- PowerShell 7:
- Windows: $HOME\Documents\PowerShell\
- Linux: ~/.config/powershell/
- macOS: ~/.config/powershell/
The directories can have several valid profile files. The difference between the profiles is based on the host application that launches PowerShell. We use ISE and VS Code as examples for hosts:
- All hosts: profile.ps1
- ISE: Microsoft.PowerShellISE_profile.ps1
- VS Code: Microsoft.VSCode_profile.ps1
If a user launches VS Code, then the following profiles could run on Windows:
- All users, all hosts: $PSHOME\profile.ps1
- All users, VS Code: $PSHOME\Microsoft.VSCode_profile.ps1
- Current user, all hosts: $HOME\profile.ps1
- Current user, VS Code: $HOME\Microsoft.VSCode_profile.ps1
If any of those files don't exist, PowerShell skips that profile.
How to access your PowerShell profile
The easiest way to retrieve the PowerShell profile is through PowerShell itself. You don't need to remember the profile paths because they are stored in the $Profile variable.
![PowerShell profile location](https://cdn.ttgtmedia.com/rms/onlineimages/ahowell_psprofile_1-f_mobile.jpg)
The only path displayed is the current user's PowerShell host application profile. The $Profile variable has additional properties to show us the other paths, which you find by piping the variable to the Get-Member cmdlet:
$Profile | Get-Member -MemberType NoteProperty
![Multiple PowerShell profile paths](https://cdn.ttgtmedia.com/rms/onlineimages/ahowell_psprofile_2-f_mobile.jpg)
To find the path for all users, all hosts profile, run the following command:
$Profile.AllUsersAllHosts
![PowerShell 7 profile](https://cdn.ttgtmedia.com/rms/onlineimages/ahowell_psprofile_3-f_mobile.jpg)
The hosts profile file is in the Program Files hierarchy and can't be edited without administrative permissions. Instead, focus on the current user, current host profile, which you find with the following command:
$Profile.CurrentUserCurrentHost
![current user, current host PowerShell profile](https://cdn.ttgtmedia.com/rms/onlineimages/ahowell_psprofile_4-f_mobile.jpg)
To edit that script, run the following command to launch VS Code and open that file:
code $Profile.CurrentUserCurrentHost
How to customize the PowerShell profile
Due to the PowerShell profile's flexibility, you have a range of customization options to suit how you want PowerShell to work.
Customize your PowerShell prompt
We could dedicate a whole article to this topic and still not cover all the possibilities to modify the PowerShell prompt. If you aren't familiar with customizing your PowerShell prompt, you can find many examples of cool prompts to see what's possible. This article covers a basic example.
For instance, to place the cursor on the line below the path and display the # symbol if we are running as the administrator on Windows, you can do the following:
Function Prompt {
$endChar = '>'
# check if running as admin
If
(([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$endChar = '#'
}
@"
$PWD
PS$endChar
"@
}
To make this function run every time PowerShell loads, put it in your profile.
How to load a PowerShell module and set defaults
A mistake that I regularly make is using a module that requires authentication before I've authenticated. Some of the modules have checks that remind you to authenticate, while others throw a cryptic error. If this problem occurs for a module you use frequently, then consider adding the authentication steps to your profile.
For this example, we use the Microsoft.Graph module, which enables you to authenticate with cached credentials. Once you have authenticated, you can then add the following command to your profile:
Connect-Graph
There is a quirk in Microsoft.Graph.Users that requires you to specify the default properties to return multiple properties of users. You can work around this by building a variable in your PowerShell profile:
$graphUserProps = @(
'BusinessPhones',
'DisplayName',
'GivenName',
'Id',
'Mail',
'PreferredLanguage',
'Surname',
'UserPrincipalName'
)
If you need to add more properties for a Microsoft Graph user, then you can reference that variable and add additional properties:
Get-MgUser -UserId <upn> -Select ($graphUserProps + 'AccountEnabled','UsageLocation')
How to add aliases and argument completers
Another excellent use for a PowerShell profile is to add an alias. If you work interactively with PowerShell, then aliases can save you a lot of typing. Using the previous Microsoft.Graph example, you could shorten Get-MgUser to gmu:
New-Alias -Name gmu -Value Get-MgUser
However, one of my favorite aliases is for kubectl, the Kubernetes command-line tool for working with clusters. I use the letter k as my alias:
New-Alias -Name k -Value kubectl
One of the perks of kubectl is it includes an autocompletion for PowerShell. If you use kubectl frequently, then you can follow the instructions at this link to add this feature to your profile.
How to add custom functions to your PowerShell profile
As you develop your PowerShell abilities, you might write more one-off functions to help with your work. But you might not feel the code is worth adding to a module. After you put your code in a source control platform, such as GitHub, then you can use dot sourcing to load the code when running PowerShell:
. C:\path\to\Function.ps1
This is just the start of what is possible. Start thinking about the commands you run frequently, and consider the other ways you can customize your PowerShell profile to make your life easier.
Anthony Howell is an IT expert who is well versed in multiple infrastructure and automation technologies, including PowerShell, DevOps, cloud computing, and the Windows and Linux OSes.