PowerShell Profiles and Useful Aliases

I’ve recently discovered the power of PowerShell Profiles.  Profiles allow you to define pre-configured settings in your PowerShell environment.  You can leverage four different types of profiles in PS:

  1. %windir%\system32\WindowsPowerShell\v1.0\profile.ps1:  Applies to all users and shells.  This means that you can specify settings for cmd.exe as well. 
  2. %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1: Applies to all users, but only works for PowerShell.
  3. %UserProfile%\My Documents\WindowsPowerShell\profile.ps1: Applies to specific users, and works for all shells. 
  4. %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1: Applies to the current user in PowerShell.  You can see the path within PS by typing the powershell variable $profile.

I personally prefer to use option 1 on my computer as I’m the only one who accesses it, and because I run PS under a different account than the one I log in with.  Option 2 would probably be the best solution for defining standard variables and aliases on servers within your organization. 

That brings me to the second half of my post.  The thing I like most about profiles is the ability to set custom aliases for myself. These aliases tie back to scripts that I re-use on a regular basis, which I keep in an “alias” directory that I’ve created in my PowerShell scripts directory.  Here are some examples:

Loading the Active Directory Module for Powershell

I use the AD module for PS a lot.  Loading it is simple enough, and its not like it takes a lot of keystrokes to do so, but I do it regularly enough that it bothers me to type so much.  Additionally, I don’t like to keep it loaded at all times, so I simply created a script called “load-activedirectory.ps1” that issues the command for me and tied it to the command “ad” in my profile using the following command:

set-alias ad c:\powershell\alias\load-activedirectory.ps1

Now whenever I want the AD module I just type “ad” and I’m good to go. 

Loading VMWare PowerCLI

This is the same principal as the AD module, but requires additional steps to load so it’s more annoying.  I almost always conenct to the same vCenter instance, so I entered all the parameters into a script called “load-powercli.ps1” and tied it to the alias “vmware” using:

set-alias vmware c:\powershell\alias\load-powercli.ps1

Opening Renite Desktop Connections

Sometimes you just need the GUI, but I work fast and don’t want to leave PS if I don’t have to.  I wanted to be able to quickly launch an RDP session to a specific server straight from PS.  To speed myself up I created a script called “open-rdpconnection.ps1” that accepts the server name as a parameter and then loads it into the mstsc syntax.  I then created another alias called “rdp”:

set-alias rdp c:\powershell\alias\open-rdpconnection.ps1

Whenever I want to enter an RDP session I simply type “RDP [computername]” and I get the prompt I want. 

As you can see, the set-alias command is pretty powerful when combined with profiles.  Yet another thing made easier by PowerShell. 

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: