Windows

How to use a PowerShell Profile to simplify tasks

Derek Schauland shows you how to create a PowerShell Profile to store your frequently used functions and work more efficiently in Windows.

Lately I have been spending a good amount of time getting to know Microsoft Windows PowerShell to help me manage my Windows environment. So far, it is one of the coolest things I have taken on in quite some time. Last time around I created a function to look up the last time a specified user set their password and the number of days until expiration. Normally, that wouldn't be terribly useful, but after a few calls to the help desk about when a password expires this little gem comes in quite handy.

As you can see there is quite a bit of code to enter on the command line to create and use this function.  Enter it once and the code is ok for the duration of your session, close the PowerShell window and you will need to re-enter the entire thing to use it next time around. Unless you create a PowerShell Profile script, then you can put commonly used things in there and use them anytime you start PowerShell.

Note: A PowerShell profile is saved on the machine where it is created, however you can copy it to other machines you work on to save time there as well.

Getting started with a profile

To begin with, you should check with PowerShell to see if a profile exists, this way if there is a profile, you can edit it rather than creating a new one. Doing so is as simple as entering Test-path $profile on the shell command line. The result will be true if the path exists and false if it does not.

Note: The $profile variable is created by default to hold the profile script file path; this makes accessing it quite easy.

If the result of test-path $profile is false you will need to create a profile; to do that, enter the following on the PowerShell command line:

New-item -path $profile -type file -force

This will create the profile script and allow you to call it for editing by entering notepad $profile on the PowerShell command line.

Windows XP is not as picky about profile creation as later versions of Windows. You may be able to simply enter notepad $profile to open the file in Notepad and be prompted to create it if it does not exist. However in later version of Windows (Vista and later) the path may not exist, in which case, use the New-item command above.

Once you create the profile and open it for the first time in Notepad, you get a standard blank file with the filename Microsof.Powershell_profile.ps1. This is where the fun begins and the creation of settings you want to use gets done.

For simplicity's sake, the profile I am going to create here isn't going to do everything you might want/need, but it will certainly get you started, so let's get going.

Active Directory

Many administrators manage Active Directory and have a few habits when doing so; PowerShell can help with this and the cmdlets are pretty simple to pull in. You will need to enable the remote server administration toolkit on non-domain controller machines for these tools to work. In Windows 7 the RSAT features are enabled as a feature.

Open your newly created profile script in Notepad by entering notepad $profile on the command line. If you haven't been playing with the profile script, it should still be empty.

Add a comment line to tell yourself what you are doing in this particular section (commenting is extremely helpful, especially if you do not do much coding), by entering a line beginning with a hash mark. On the line below that, enter the command you want the profile script to execute.

#Importing Active Directory Module
Import-Module ActiveDirectory

This will import the Active Directory cmdlets for use on your client machine. Once you have this saved in the profile script, enter .$profile on the command line to reload your profile.

Note: You may see an error when executing the Profile script referencing remote signing. PowerShell does not allow script execution by default. To enable this you will need to change the execution policy with the following command:
Set-executionpolicy -scope currentuser -executionpolicy remotesigned

This will prompt you to set the execution policy for the logged on user to remotesigned allowing that user to run scripts. Press enter to accept the default choice (yes).

With this command executed you should be able to execute .$profile on the command line to reload your profile and see the module being imported for use in PowerShell.

Functions

Since we have looked at creating a PowerShell function to get password information, I am going to add that function to a profile to make it available as soon as PowerShell starts.

The version of the function I am going to add includes a couple more features to remind the administrator to change the password on non-expiring accounts if they are older than x days; other than that, it is the same as the script displayed in Figure A. Figure A

A PowerShell Profile (click to enlarge). Download the full PowerShell Profile script here.

Another function you might add to your profile is the Prompt function. This will let you create a custom command prompt for your PowerShell sessions.

To add the prompt function, add the following to your profile:

function prompt {"PS [$env:computername] >"}

This can be entered on one line, or many depending on how much customizing you want to do. In the example above, the Prompt appears with PS and the name of the computer I am using displayed in brackets, for example, PS [My computer] >.

Aliases

You may have noticed I entered notepad on the command line to open the notepad.exe application. Notepad is a PowerShell alias to the notepad executable which allows it to open by simply typing notepad and pressing enter. You can create aliases for almost anything in PowerShell, you can even create shorter aliases to things that exist already.

To add an alias to your profile enter the following:

#Adding shorter alias for notepad
new-item alias:np -value c:\windows\system32\notepad.exe

This code creates a new item of the type alias that has a name of np. The value is the path to the program or file you want to reference with the alias.

Once saved and reloaded, your profile script will now allow you to open Notepad by entering np on the command line. To edit your profile in the future, you can enter np $profile on the command line and you should see your profile script appear in a Notepad window.

When adding things to your profile, consider only the things you will use often. Doing this will keep your profile manageable. Hopefully adding a profile script to PowerShell will speed it up for you as it has for me.

About

Derek Schauland has been tinkering with Windows systems since 1997. He has supported Windows NT 4, worked phone support for an ISP, and is currently the IT Manager for a manufacturing company in Wisconsin.

5 comments
jsneddon
jsneddon

I recently started working more with profiles and found it a little frustrating maintaining profiles across multiple servers and came up with a script to solve the problem:http://sneddo.net/powershell-profiles/ Still a bit of a work in progress, but you get the idea :)

Derek Schauland
Derek Schauland

Hey everyone, I will get the script linked soon, hopefully today. Since my profile script is ever changing there might be a few more things in it than outlined here, but maybe it will spark some conversation Derek

JRJS
JRJS

I must agree with techn0gichida - having a graphic of only part of the script is disappointing. Please provide a graphic of the whole script or, better, provide a link to a script listing.

techn0gichida
techn0gichida

The graphic provided does not show all of the scripting either horizontally or vertically. While I'm all for guessing that usually leads to trouble.

Editor's Picks