/config/powershell/powershell-profile
$PROFILE: where the PowerShell profile is and how to create it
- tool
- PowerShell
- file
- $PROFILE (Microsoft.PowerShell_profile.ps1 in the user's Documents folder)
- key
- $PROFILE.CurrentUserCurrentHost
- type
- path to a .ps1 script run at startup
- default
- not created; Documents\WindowsPowerShell\ (5.1) or Documents\PowerShell\ (7)
A PowerShell profile is a script that runs every time PowerShell starts, used for aliases, functions, prompt changes and module imports. $PROFILE holds the path of the most common one, current user, current host. The file does not exist until you create it:
if (-not (Test-Path $PROFILE)) { New-Item -Path $PROFILE -ItemType File -Force }
notepad $PROFILE-Force also creates the missing WindowsPowerShell or PowerShell folder. Changes take effect in new sessions, or immediately after . $PROFILE.
The four profiles
$PROFILE also has a property for each of the four profile paths:
$PROFILE.PSObject.Properties | Where-Object Name -like '*Host*' | ForEach-Object {
'{0,-24} {1}' -f $_.Name, (Split-Path $_.Value -Leaf)
}
"CurrentUserCurrentHost is in a folder named: $(Split-Path (Split-Path $PROFILE -Parent) -Leaf)"Output
AllUsersAllHosts profile.ps1 AllUsersCurrentHost Microsoft.PowerShell_profile.ps1 CurrentUserAllHosts profile.ps1 CurrentUserCurrentHost Microsoft.PowerShell_profile.ps1 CurrentUserCurrentHost is in a folder named: WindowsPowerShell
| Property | Windows PowerShell 5.1 | PowerShell 7 (Windows) |
|---|---|---|
| CurrentUserCurrentHost ($PROFILE) | Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 | Documents\PowerShell\Microsoft.PowerShell_profile.ps1 |
| CurrentUserAllHosts | Documents\WindowsPowerShell\profile.ps1 | Documents\PowerShell\profile.ps1 |
| AllUsersCurrentHost | $PSHOME\Microsoft.PowerShell_profile.ps1 | $PSHOME\Microsoft.PowerShell_profile.ps1 |
| AllUsersAllHosts | $PSHOME\profile.ps1 | $PSHOME\profile.ps1 |
"Host" is the program that hosts PowerShell: the console, the VS Code PowerShell extension (Microsoft.VSCode_profile.ps1) and the ISE (Microsoft.PowerShellISE_profile.ps1) each have their own current-host profile. Put settings for all of them in CurrentUserAllHosts.
On Linux and macOS, PowerShell 7 uses ~/.config/powershell/Microsoft.PowerShell_profile.ps1.
Things that surprise people
- OneDrive folder redirection: when OneDrive backs up Documents, the profile path is under
OneDrive\Documents\.... Always use$PROFILEinstead of a typed path. - 5.1 and 7 have separate profiles. A profile in
WindowsPowerShellis not read bypwsh, and the reverse. - Execution policy: the profile is a script, so with the
Restrictedpolicy it fails to load with running scripts is disabled on this system. - Skip it:
powershell -NoProfile(orpwsh -NoProfile) starts without any profile. Scripts run by scheduled tasks and CI should use it, so they do not depend on a user's profile.
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh