About profile scripts
Aliases do not persist across PowerShell sessions. A profile script is often used for user-specific preferences like this.
Profiles are where most user-specific shell customization takes place, from changing prompts and default settings to loading modules to creating aliases.
Shell customization with profile scripts is an enormously involved and highly personal topic. This section provides a brief introduction to the topic only.
PowerShell has four different profile paths – two are user-specific and two are machine-specific. The profile paths are also dependent on the host – one user and one machine path are specific to the terminal that started PowerShell.
The profile paths are described by the built-in variable $profile
. The Select-Object *
command is used to show all the possible values in the example below:
PS> $profile | Select-Object *
AllUsersAllHosts : C:\Program Files\PowerShell\7\profile.ps1
AllUsersCurrentHost : C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\user\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\user\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
If the same command is run in the VS Code integrated terminal, the two host-specific paths will differ:
PS> $profile | Select-Object *
AllUsersAllHosts : C:\Program Files\PowerShell\7\profile.ps1
AllUsersCurrentHost : C:\Program Files\PowerShell\7\Microsoft.VSCode_profile.ps1
CurrentUserAllHosts : C:\Users\user\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\user\Documents\PowerShell\Microsoft.VSCode_profile.ps1
PowerShell will run each of the profile scripts where the script exists. If the script does not exist, the path is ignored.
Starting PowerShell with the -NoProfile
switch avoids loading profile script files, a useful parameter for any scheduled scripts, or for testing to see if the profile is causing a problem.
The paths used above may not exist at all. To create a profile script, create the directory first, if necessary, then save the script file using one of the names and paths above.
The about document for profiles explores this topic further:
Get-Help about_profiles
A variety of different tools are available to customize the look and feel, which are typically started from a profile script. Customization of the prompt is a common activity:
- PowerLine: https://github.com/Jaykul/PowerLine
- oh-my-posh: https://ohmyposh.dev/
- Sarship: https://starship.rs/
The Windows Terminal customized prompts article makes use of oh-my-posh in an example setup:
https://learn.microsoft.com/en-us/windows/terminal/tutorials/custom-prompt-setup.
Finally, for users with multiple computers, the chezmoi tool might be used to ensure all computers use the same configuration:
Commands (and aliases) use parameters to pass arguments into a command.