Powershell Profile¶
Your PowerShell profile is the script that runs every time a session starts. Keeping it in a gist means a new machine is one download away from feeling like your own.
Open PowerShell as administrator¶
Persistent environment variables¶
$env:NAME = 'value' only lives as long as the session. To make a variable survive a restart, write it to the user's environment block.
function Set-EnvVar {
param($EnvName, $EnvValue)
[System.Environment]::SetEnvironmentVariable($EnvName, $EnvValue, [System.EnvironmentVariableTarget]::User)
$env:$EnvName = $EnvValue # also set it for the session you are in
Write-Host "Set $EnvName"
}
Set-EnvVar 'PSGIST' '<your-gist-id>'
Read it back with $env:PSGIST. The User target writes to the current account; use Machine for all users, which needs an elevated session.
| Target | Scope | Needs admin |
|---|---|---|
Process |
This session only | No |
User |
Current account, persists | No |
Machine |
Every account, persists | Yes |
Where the profile lives¶
$PROFILE # full path to the current host's profile
$PROFILE | Select-Object * # every profile path PowerShell knows about
Split-Path $PROFILE | Set-Location
If the file does not exist yet, create it along with any missing folders.
Pull the profile from a gist¶
Keep the canonical profile in a gist, store its id in an environment variable, and fetch it on a new machine.
function Get-GitProfile {
$gitUser = 'bamr87'
$masterProfile = 'Microsoft.PowerShell_profile.ps1'
$url = "https://gist.githubusercontent.com/$gitUser/$env:PSGIST/raw/$masterProfile"
Invoke-WebRequest -Uri $url -OutFile $PROFILE
Write-Host "Profile written to $PROFILE"
}
Invoke-WebRequest replaces the older New-Object System.Net.WebClient pattern and reports failures as terminating errors you can catch.
Reload after editing¶
A full restart is sometimes cleaner, especially after changing environment variables.
Execution policy¶
A downloaded profile will not run under the default policy on Windows. Allow local scripts while still requiring signatures on remote ones.
See also¶
- PowerShell cheatsheet β the broader command reference
- Terminal shortcuts β keyboard movement that works in most shells