PowerShell Profiles and Customization
A PowerShell profile is a script that runs automatically when you start PowerShell. You can use the profile to customize your environment, add aliases, functions, and variables that you want available in every session.
Parent Topic: PowerShell Scripting and Programming Related: PowerShell Comprehensive Guide
1. What is a Profile?
Technically, a profile is just a .ps1 script file. PowerShell checks specific locations for these files upon startup and executes them if they exist.
2. The $PROFILE Variable
The $PROFILE automatic variable stores the path to the profile for the current user and the current host application (e.g., VS Code, Windows Terminal, Console).
# View the path to your current profile
$PROFILE
# View all possible profile paths for the current session
$PROFILE | Select-Object *
Profile Locations (Precedence Order)
There are actually four different profiles that can load. They load in this order (later ones overwrite earlier ones):
- All Users, All Hosts: Applies to everyone on the computer, in any PowerShell host.
- Path:
$PSHOME\Profile.ps1
- Path:
- All Users, Current Host: Applies to everyone, but only in the specific host (e.g., only in VS Code).
- Path:
$PSHOME\Microsoft.PowerShell_profile.ps1
- Path:
- Current User, All Hosts: Applies to you, in any PowerShell host. (Most Common)
- Path:
$Home\Documents\PowerShell\Profile.ps1
- Path:
- Current User, Current Host: Applies to you, only in the specific host.
- Path:
$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
- Path:
3. Creating Your Profile
By default, the profile file might not exist. You need to create it.
# Check if the profile file exists
if (!(Test-Path -Path $PROFILE)) {
# Create the file (and directory if needed)
New-Item -ItemType File -Path $PROFILE -Force
}
# Open the profile in your default editor (e.g., VS Code or Notepad)
psedit $PROFILE
# OR
code $PROFILE
# OR
notepad $PROFILE
4. Common Customizations
Add these to your profile script to have them available every time you open PowerShell.
Custom Prompt
You can override the prompt function to change how your command line looks.
function prompt {
$time = Get-Date -Format "HH:mm:ss"
Write-Host "[$time] " -NoNewline -ForegroundColor Cyan
Write-Host "$PWD" -NoNewline -ForegroundColor Green
return "> "
}
Aliases
Shortcuts for commands you use often.
Set-Alias -Name np -Value notepad
Set-Alias -Name g -Value git
Custom Functions
Define quick utility functions.
function Get-MyIP {
(Invoke-RestMethod "https://api.ipify.org").Trim()
}
Importing Modules
If you always use specific modules (like Azure or Active Directory), import them here. Note that this might slow down startup time slightly.
Import-Module Pester
Import-Module PSReadLine
5. Reloading the Profile
If you make changes to your profile, they won't apply to the current session automatically. You have two options:
- Restart PowerShell.
- "Dot source" the profile to reload it.
. $PROFILE
6. Execution Policy Warning
Since the profile is a script, your Execution Policy must allow scripts to run. If it is set to Restricted, your profile will not load.
# Check policy
Get-ExecutionPolicy
# Set to RemoteSigned (Recommended for local scripts)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
7. Cross-Platform Considerations (PowerShell 7)
If you use PowerShell 7 (Core) on Windows, Linux, and macOS, your profile location might differ.
- Windows:
C:\Users\Username\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 - Linux/macOS:
~/.config/powershell/Microsoft.PowerShell_profile.ps1
You can write cross-platform logic in your profile:
if ($IsWindows) {
Set-Alias -Name list -Value Get-ChildItem
} elseif ($IsLinux) {
# Linux specific settings
}
Return to PowerShell Scripting and Programming