Getting Started with PowerShell Profiles

This article is going to show you how easy it can be to create a PowerShell profile and start using it to make your administration go much more smoothly.

What are PowerShell Profiles?

Your PowerShell profile is not some mysterious collection of settings buried deep in the registry. You don’t need any more skill or expertise to use your PowerShell profile than you need to use anything else in PowerShell.

In simple terms, your PowerShell profile is your own personal script that runs each time you start PowerShell. Your PowerShell profile also serves as a place for you to load consistent settings, drives, and custom commands.

When you use PowerShell, there’s a variable called $profile. It references a location on the hard drive that contains your profile. Go ahead, and look at it. It’s been sitting on your computer the entire time.

The PowerShell profile variable. (Image Credit: Michael Simmons)
The PowerShell profile variable. (Image Credit: Michael Simmons)

There are two things worth pointing out here:

  • PowerShell_ISE is in the filename. This is important because there are actually two profiles, one for the PowerShell console and one for PowerShell ISE.
  • .PS1 file extension. This is important because you should recognize that the file extension represents a PowerShell script, and that’s exacty what the profile is.

Knowing that you’ve got a script that runs each time you start PowerShell, you may already be thinking “Actually, I could really use that to do x, or y, or x AND y!” If you’re looking for some inspiration, read on as I show you three simple things that I use my profile to customize the look and operation of my PowerShell sessions.

Creating Your PowerShell Profile

You’ve already seen that PowerShell has been trying to load a profile each time a new session is launched. Even if you haven’t created your PowerShell profile, it’s not too late because the file doesn’t exist. If you want to begin using a profile, you’ll need to create a new file.

I create the profile using the New-Item cmdlet, which can be used to create files or directories. If you already have a profile, then you can take advantage of the Test-Path cmdlet and testing true or false to determine whether the profile exists.

If (Test-Path $profile –eq $False) {New-Item –ItemType File –Path $profile -Force }

Note that you can enter this code directly in PowerShell session to execute. Simply copy and paste it into your session.

Although the file is a little different between the PowerShell console and ISE, those changes are reflected in the value of the $Profile variable, so this code works in both the console and ISE.

Add it into each one to create your profile.

Editing Your PowerShell profile

Now that you have a profile, you can edit it just like any other PowerShell script. You can use your favorite PowerShell script editor, but I think PowerShell ISE is great choice.

Use this command to open your profile in the ISE:

ISE $profile

Three Great Uses with the PowerShell Profile

Having a profile is all well and good, but it’s not going to help you unless you have a good use for one. Here are some great uses for profiles that might get you thinking.

Set an alternate credential

You probably have two accounts, the account you log into and your administrative account. If that’s the case, then you’re likely familiar with the problem of having to constantly use administrative accounts while you’re logged into your regular everyday account.

$Global:AdminMe = Get-Credential –Username “<domain\adminaccount>”

The downside of this approach is that you will now have to login every time you start PowerShell. But I still like it because I can avoid having to sign in multiple times throughout the session because when I need to access a resource as my administrator account I just need to use the -Credential $AdminMe parameter.

Map PowerShell drives

Do you frequent certain network shares throughout the day? If you have a central file server that houses your utilities and tools, why not map to it directly from PowerShell?

New-PSDrive –Name “Tools” –PSProvider “FileSystem” –Root “\\Server\Fileshare” –Credential $AdminMe

Now you’ll always have your tools directory at hand, and did you notice the $AdminMe property? See how handy it is?

Create or load a function

Do you have a process that takes multiple steps and you want to automate it? We’ve got several articles on Petri on creating a function, such as Creating a Function to Test and Compare PowerShell Commands and Creating Advanced Functions in PowerShell.

Now usually the best place for it is in an external script that can be shared. But if you have something that is just special for you, then go for it, and add it to your profile.

Function “stop-ie” {
Get-Process –Name “Internet Explorer” | Stop-Process

If it’s something that you have to do frequently, and it can save you some typing, then create some functions to take care of your tasks.

If you have decided to share your function and now it is on your tools share as an appropriately named stop-ie.ps1, then you can load the function like this:

                  . \\Tools\Stop-IE.ps1

The important part of that is “.”, which is called dot-sourcing and it means to run the file, rather than treating the filename as a string.

Another important thing to remember is that if you’re loading functions from a script, you’ll have a real surprise if it’s not actually stored in a function inside the script.

By that, I mean if the Stop-IE script does not load the function, but instead stops all IE processes, you may have just closed your browser session when you didn’t want to.