$Profile - profile.ps1

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.

Below are a few functions you may want 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}

PowerShell may also be configured for multiple power users, 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.

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


dotfiles.org - Share startup files
Custom PowerShell Prompts - PowerShell For Fun

© Copyright SS64.com 1999-2013
Some rights reserved