PowerShell profile is essentially a regular PowerShell script (PS1) that runs when PowerShell starts and is used as a logon script to configure an environment. You can add your own functions, commands, and aliases, import PowerShell modules, set environment variables, change the appearance and settings of a PoSh console using a PowerShell profile. All profile elements will be automatically applied to each new PowerShell session.
There are multiple paths in Windows PowerShell to store profile files. The table below shows all paths by their priority (the highest priority profile comes first):
Path | Description |
$PSHOME\Profile.ps1 | AllUsersAllHosts |
$PSHOME\Microsoft.PowerShell_profile.ps1 | AllUsersCurrentHost |
$Home\Documents\PowerShell\Profile.ps1 | CurrentUserAllHosts |
$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 | CurrentUserCurrentHost |
If you want to configure a PowerShell session settings for all users of a computer, you must use the $PROFILE.AllUsersAllHosts
file. To configure the PowerShell profile for the current user only, save your configuration to $PROFILE.CurrentUserCurrentHost
.
To find your PowerShell profile, just run the $Profile
command. By default, the path to a user PowerShell profile is C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
- In Windows PowerShell, the
$Home
environment variable refers to the current user profile folder (C:\Users\username).$PsHome
refers to the directory PowerShell is installed to (C:\Windows\System32\WindowsPowerShell\v1.0). - In new PowerShell Core 7.x versions, the
$PSHome
refers to C:\Program Files\PowerShell\7. - In PowerShell Core for Linux, the profile is located in
/opt/microsoft/powershell/profile.ps1
or/usr/local/microsoft/powershell/7/profile.ps1
.
PowerShell ISE has its own profile files:
$PsHome\Microsoft.PowerShellISE_profile.ps1 | All users |
$Home\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1 | Current user |
Visual Studio Code also has its own profiles if used as an editor of PowerShell scripts):
$PSHOME\Microsoft.VSCode_profile.ps1 | All users |
$Home\Documents\PowerShell\Microsoft.VSCode_profile.ps1 | Current user |
All paths to profiles can be found in the $PROFILE environment variables
$PROFILE | Get-Member -Type NoteProperty
To access a profile file (for example, the current user profile):
$PROFILE.CurrentUserCurrentHost
or $PROFILE
To check if the PowerShell profile of the current user has been created, run the command below:
Test-Path -Path $PROFILE
In this example, a PowerShell profile file for the current user has not been created (False
).
To create a PowerShell profile file, if it does not exist, use the script below:
if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
You can edit it with any text editor. For example, you can edit a profile file using:
- Notepad:
notepad $profile
- PowerShell ISE:
ise $profile
- Visual Studio Code:
code $profile
Let’s try to configure custom PowerShell profile. In this example we will adjust the color, the console title (it will display the PowerShell version), change the default PowerShell directory, and display the computer name at the PS console prompt:
function CustomizePSConsole {
$Host.ui.rawui.foregroundcolor = "cyan"
$hostversion="$($Host.Version.Major)`.$($Host.Version.Minor)"
$Host.UI.RawUI.WindowTitle = "This is PowerShell $hostversion"
Set-Location 'C:\PS\'
Clear-Host
}
CustomizePSConsole
Let’s show a computer name in the PS console prompt:
function Prompt
{
$env:COMPUTERNAME + " | " + (Get-Location) + "> "
}
For example, you can map a network drive:
New-PSDrive –Name “Tools” –PSProvider "FileSystem" –Root "\\MUN-DEV01\Scripts"
If you often use PowerShell to manage Azure tenants or Microsoft 365, you can display a prompt to connect to your tenant each time you start the console. If you type Y, the following script will prompt you to enter your credentials and connect to your Exchange Online tenant:
$connectM365= Read-Host "Connect to Exchange Online? (Y/N)"
If ($connectM365 -eq "Y"){
$LiveCred = Get-Credential
Connect-ExchangeOnline –Credential $LiveCred
}
You can add the Clear-Host
cmdlet to the end of your profile file to clear the console. Save the profile file as Microsoft.PowerShell_profile.ps1 and restart PowerShell. At the next start, all profile settings will be automatically applied to your console and its appearance will change.
Set-ExecutionPolicy Remotesigned
If you want your PowerShell profile to be applied to remote sessions as well, use the command:
Invoke-Command -Session $s -FilePath $PROFILE
–NoProfile
option when running PowerShell.exe
or pwsh.exe
.
1 comment
https://ohmyposh.dev/ FTW 😉