Posted by: brainscott | 11/12/2008

Customizing PowerShell with a profile

Windows PowerShell can be customized with a profile. A profile is just a PowerShell script that you have to save in a specific location. For detailed information about profiles, you should check out

Basically, there are four different profiles:

  • profile.ps1 in %windir%\system32\WindowsPowerShell\v1.0: this profile applies to all users and all shells.
  • Microsoft.PowerShell_profile.ps1 in the same directory: this profile applies to all users but only to the Microsoft.PowerShell shell.
  • profile1.ps1 in your My Documents\WindowsPowerShell folder: this profile only affects your user but all shells.
  • Microsoft.PowerShell_profile.ps1 in the same folder: this profile only affects your user and only the Microsoft.PowerShell shell.

If you have installed the PowerShell Community Extensions and you did a default installation, you already should have a profile.ps1 file under <MyDocuments>\WindowsPowerShell. In Vista, the location would usually be c:\Users\<username>\Documents\WindowsPowerShell.

If you have a default installation of Windows PowerShell you need to create these files yourself. Let’s go ahead and create a profile.ps1 file. Of course, we will do it in PowerShell just for the fun of it and add some twists. Use the following commands:

  1. $powershelldir=”$env:personal\WindowsPowerShell”
  2. if ( (test-path $powershelldir) -ne $true) {mkdir $powershelldir}
  3. new-item $powershelldir\profile.ps1 -ItemType file
  4. notepad $powershelldir\profile.ps1

A bit of explanation is probably in order. In step 1, you get the path to your Documents folder from an environment variable called personal. That works in Vista but not in earlier versions of Windows like Windows XP. In XP, use “${env:/userprofile}\My Documents\WindowsPowerShell”. In step 2, the cmdlet test-path is used to verify if the folder path you saved in $powershelldir exists. If not, the folder is created with mkdir. In step 3, the new-item cmdlet is used to create profile.ps1 in the folder and with step 4, you just open the file in notepad. Of course, you could have used Windows Explorer to go to or create the WindowsPowerShell folder and then create the profile.ps1 file but where’s the fun in that?

Now that you have the profile.ps1 file open in notepad, what can you do with it? Well, you can add aliases, custom functions, anything you like. Let’s add a custom function that displays only directories in a folder like you do with the “dir /AD” command in cmd.exe. Add the following text to the profile.ps1 file:

function ddir { dir | ? {$_.Mode -like “d*”} }

Now save the profile.ps1 file and quit notepad. Because the profile.ps1 file is loaded when PowerShell starts, quit PowerShell and restart. If you have just installed PowerShell, it is highly likely that you will see the following error:

By default, PowerShell restricts the use of unsigned scripts. To get passed that, run the following command: set-executionpolicy remotesigned. On Vista, be sure to run PowerShell as Administrator before you run that command.

When you start PowerShell, the profile.ps1 file will be executed and the ddir function should be available. Try it by typing “ddir” at the command prompt. You should only see directories in the result.

You might wonder why a function was made and not an alias. The reason is that in this version of PowerShell, aliases can only be made for a command without parameters.

To really understand what you can do with a profile, I suggest you use the PowerShell Community Extensions and check out their profile.ps1.

Article by: psshell



  1. Topics include how to create Windows PowerShell scripts that use SQL Management Objects (SMO) to manage and program SQL Server. Article Directory

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: