home > notes > powershell > Profile [ printer-friendly version ]
Basics
Profile
Simple GUI
ShowUI

PowerShell Profile

GOAL: Identify and create your PowerShell profile file, define an alias for a command, and put that alias definition in your profile so that it persists across PowerShell sessions.

If you're familiar with Unix, you are probably familiar with the idea of a profile file - a script that executes every time you open a terminal window or log in to the system. Windows PowerShell implements this functionality, and you can locate and edit your profile to automate environmental and setup tasks.

See the path to your profile

To see where your profile file is located, just print out the value of the $profile variable:

$profile
    
On my machine, this is the result:
C:\Users\{me}\Documents\Windows\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
    

You can see that this file doesn't actually exist (yet) with the test-path command:

test-path $profile
    
The test-path command returns False if the file doesn't exist.

Alias a command

You can set an alias for a PowerShell command. Let's try a trivial example:

Set-Alias boing dir
    
Now, you can type boing and get a directory listing - but only for the current PowerShell session. Handy, but cumbersome if you want to use boing to get a directory listing in future PowerShell sessions.

Put the alias command in your profile

If your profile file does not exist, you can create it this way:

New-Item -Path $profile -ItemType file -Force
    
Once the file has been created, editing it in Notepad is easy:
notepad $path
    
Add the Set-Alias command to your profile script:
Set-Alias boing dir
    
Save the profile file and exit Notepad.

Verify that your alias command executes automatically

Exit Powershell and start it up again - your boing alias should give you a directory listing. Naturally, you can add other PowerShell commands to your profile script at any time, and they'll be executed every time you start PowerShell.

Summary

By working through these steps, you've learned a number of things:

  • PowerShell supports the use of a Unix-like profile file that is invoked when you launch PowerShell
  • You can alias PowerShell commands to define your own shortcuts
  • You can add commands to your PowerShell profile that will be invoked every time you open PowerShell

home Alcohol, Tobacco, and Firearms - who's bringing the chips? privacy