How-to: Configure the PowerShell startup profile [$Profile]

Configure the PowerShell environment.
The PowerShell $Profile is run automatically when the shell session is started, it can be used to run scripts and set variables.

The $profile automatic variable will show the location of your Profile.ps1 file:

PS > "$Profile"

To edit the profile with notepad:

PS > notepad $Profile

To reload the profile into the current session:

PS > .$Profile

By default, the profile file does not exist, even though PowerShell has a filename for it, the file can be created using New-Item or notepad.

The search order of commands is Alias > Function > Cmdlet

Functions defined in $Profile will take precedence over built-in cmdlets with the same name. However to replace an Alias you must first remove the built-in Alias with Remove-Item. The built-in aliases are 'sticky', unless they are removed from every session (by adding a Remove-Item command to $Profile) then they will re-appear the next time you start PowerShell.

The Common Parameters such as -debug, -Verbose, -ErrorAction etc can also have their default set within the profile. For example $WhatIfPreference=$true
will make -whatif default to $True for all cmdlets.

UTF 8 encoding

In Windows PowerShell (but not PowerShell Core), different cmdlets use different default encodings. To set a default encoding for all cmdlets and the redirection operator, you can set a default parameter value which will apply to any cmdlet with an -encoding parameter.

$PSDefaultParameterValues = @{ '*:Encoding' = 'utf8' }

or if you already have some other default parameters set, use $PSDefaultParameterValues.Add('*:Encoding', 'UTF8')

Functions

Below are a few sample functions you may like to add to your $Profile:

'CD -' function - quickly change directory/location

List all local drives, listing only drives with > 1 MB of free space excludes any unmounted drives.
function drives{gdr -p FileSystem |where {$_.free -gt 1MB}}

Perserve Command History Across Sessions

Change background color on all elevated command prompts (from ilovepowershell.com):

if ($host.UI.RawUI.WindowTitle -match "Administrator")
{$host.UI.RawUI.BackgroundColor = "DarkRed";
$Host.UI.RawUI.ForegroundColor = "White"}

Add the word "Administrator" to the title bar of all elevated command prompts (from leastprivilege.com):
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent() $p = New-Object System.Security.Principal.WindowsPrincipal($id) if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {$Host.UI.RawUI.WindowTitle = "Administrator: " + $Host.UI.RawUI.WindowTitle}

Profile File Locations

To support configurations for multiple users on the same machine, there are 4 locations where you can store a Profile.ps1 file:

1. "All users" profile "<Installation Directory>\profile.ps1"
2. "All users," host-specific profile "<Installation Directory>\Microsoft.PowerShell_profile.ps1"
3. Current user profile "<My Documents>\WindowsPowerShell\profile.ps1"
4. Current User, host-specific profile"<My Documents>\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"

These are loaded in order 1, 2, 3, 4, it is possible to redefine the same function in the different $profile files. The last one loaded will take precedence.

“O Marvelous! what new configuration will come next? I am bewildered with multiplicity” ~ William Carlos Williams, American poet (1883-1963)

Related PowerShell Cmdlets

Custom PowerShell Prompts - PowerShell For Fun.


 
Copyright © 1999-2024 SS64.com
Some rights reserved