Craig Forrester

The Pleasure of Finding Things Out

github linkedin email rss
Interactive PowerShell, Part 3: Profiles
February 16, 2018
3 minutes read

Profiles

One of the first things I do when working interactively on a Linux system is customize a few key configuration files, one of which is .bash_aliases, so that I can work more comfortably and efficiently at the command line.

PowerShell has its own equivalent of .bashrc in which you can create your own functions, aliases, and shortcuts to make working with PowerShell as an interactive shell much easier. It’s known as a profile. I say “a” profile because there are actually 6 of them, but for the purposes of our introduction here, we’re going to talk about the one most folks are referring to when they refer to your PowerShell profile.

# Display profile location
echo $PROFILE

Sample output:

C:\Users\User\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Again, this is one of several profiles, but the primary one we want to focus on right now, as we simply want a place to store all of our customizations for interactive use.

As you can see from the output, a profile is just a PowerShell script – notice the .ps1 file extension. This is the path where PowerShell expects your profile to be, however it may or may not be there when you go to edit it, so the first thing we need to do is check to see if it exists.

Creating a Profile

You can check to see if your profile exists one of several ways, but the simplest is just to use Test-Path like this:

# Check to see if the file on the $PROFILE path exists
Test-Path $PROFILE

If it exists, then Test-Path will return True and, if not, False. Since the file is in a subdirectory, WindowsPowerShell, that may not itself exist, we can’t just type notepad $PROFILE and edit it.

# Creates the file, and the subdirectory too, if neither exists:
if (-not (Test-Path $PROFILE)) {
New-Item -Path $PROFILE -Force
}

Important: Do not run New-Item with the -Force switch without first being certain the file does not exist, or you will overwrite any existing profile with an empty file! You have been warned!

Now we can edit it in Notepad:

# Open the current user's profile in Notepad
notepad $PROFILE

By default, this file should be empty. We can start filling it up with some really helpful goodies right away.

Bootstrapping Your PowerShell Profile

A PowerShell profile is essentially a script that is run when you first launch the shell. Being a script, it can include just about anything that you would include in a script: variables, functions, code from other modules, etc.

Here are a couple of ideas to get you started:

  • Add the ip alias to Get-NetIpConnection from my previous article.
  • Add the Get-DesktopApps function from my previous article.

As we progress through this series I’ll make more suggestions for additions to your profile, but this will get you started.

Additional Reading


Back to posts