DevHawk’s Slightly Useful Powershell Configuration

Since folks were interested in my favorite tools, I thought I’d blog how I have PowerShell configured. I’m not an ultra-power user, but I hold my own and hopefully you can use some of this configuration for yourself. Please tell me you’re not still using CMD.

First, I use a trick I picked up from Tomas Restrepo to change your home directory and profile script. Here’s my Microsoft.PowerShell_profile.ps1 file (in the WindowsPowerShell subdirectory of My Documents)

# reset $HOME and $PROFILE variables
set-variable -name HOME -value "D:HPierson.Files" -force  
(get-psprovider FileSystem).Home = $HOME  
set-variable -name Profile -Value "$HomeScripts_profile.ps1"

# Run the $PROFILE script
. $profile

By default, PS uses the user’s personal directory (c:usershpierson in my case) as the home directory and the aforementioned filename for the profile script. Personally, I like to keep all “real” data off my boot partition so that I don’t have to back it all up when I repave. So my “real” home location is d:HPierson.Files. The above script sets both the $HOME variable and file system home property to this directory. It also resets the $PROFILE variable to a script in my $homeScripts folder and runs it.

My $PROFILE script does several things of note:

  • It adds the aforementioned $homeScripts folder to the path. My utilities folder is a permanent part of the path, put I only add the scripts folder when I’m actually running PS.
  • If I’m running as administrator, I set the background color of the console window to red. I think I picked up this script from Brad Wilson at some point.
  • Set location to home, otherwise when I start PS as admin, it starts in c:winsys32.
  • I have a simple prompt script file that displays current folder, the current command number and a list of yellow plus signs indicating how deep I am in the directory stack. To get it to work, I have to remove the standard prompt function, which I do in $PROFILE.
  • I can’t ever remember the space between “cd” and “..”, so I wrote a simple function called “cd..” that executes “cd ..”.
  • I have a su function that leverages the Script Elevation PowerToys. If you pass in a command, it executes it with elevated credentials. If you just execute su, it runs an elevated PowerShell.
  • I use 7-zip for my compression needs, including the 7za command line app. However, PS has issues w/ executing an exe that starts with a number. So I aliased 7za as “zip”. Update: Tomas points out that you can prepend an ampersand to force execution, so I could have typed “&7za”. I forgot that when I created the alias and am now used to using zip, so I’m not going to change it. But I thought you should know.
  • I have an ever-changing set of aliases, depending on my needs. Currently, I alias “ipy”, “cpy”, “fsi”, “fsc”, “devenv” and “chiron” to their fully path-qualified equivalents, so I can run them from anywhere without having to add their respective folders to the path.

I don’t set vsvars in the $profile script, but I do have a copy of the one Chris Tavares wrote in my scripts folder, so I can set up a VS environment in a moments’ notice.

Also, I put PowerShell on the Vista quick launch bar, so I can bring it up by typing Win-2.

Comments:

Any chance of pasting the actual $profile for us to work on?
Would it be possible to post the actual $profile for us to see/copy/paste?
yes, I am still using CMD! (Simple reason for it, not important here.) But I have flagged this one as good things to do if and when I succumb to PowerShell. Also, these are all good things to do in my various SFA and SUA shells too, as I get more into those subsystems. (CMD and C Shell, I really am a throw-back!)
You should give the Powershell Community Extensions a look. They have functions that cover the CD.. and CD... as well as zip functions and many more. http://www.codeplex.com/PowerShellCX/Wiki/View.aspx?title=PSCX%201.1.1%20Features&referringTitle=Home
@Richard, I keep meaning to take a look at the PS Community Extensions. Just haven't made the time yet. Thanks for the heads up.