A Simple Go Function to Simplify Navigating to Popular Directories

Before PowerShell came along I had a number of doskey aliases set up like “gt” to go to the temp dir.  I have since converted those aliases to a single g function (short for “go”):

   1: function g($shortcut) {
   2:     $ht = @{
   3:         bin = "C:\Bin";
   4:         doc = "$([Environment]::GetFolderPath('MyDocuments'))";
   5:         sys = "$env:SystemRoot\System32";
   6:         t   = "$env:Temp";
   7:         win = "$env:SystemRoot";
   8:         cf  = "$env:CommonProgramFiles";
   9:         pf  = "$env:ProgramFiles";
  10:         www = "$env:SystemDrive\inetpub\wwwroot";
  11:         gac = "$env:SystemRoot\Assembly\GAC";
  12:         tfs = "C:\Tfs"
  13:         clr = "$([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())";
  14:     }
  16:     if ($shortcut) {
  17:         if (!$ht["$shortcut"]) { 
  18:             throw "$shortcut isn't valid shortcut. Execute g with no params to see valid shortcuts." 
  19:         }
  20:         cd $ht.$shortcut
  21:     }
  22:     else {
  23:         "Valid shortcuts are:"
  24:         $ht.Keys | Sort | select @{n='Shortcut';e={$_}},@{n='Destination';e={$ht.$_}}
  25:     }
  26: }


Usage is pretty simple.  Execute g to see all shortcuts and the associated location:

PS C:\> g
Valid shortcuts are:

Shortcut    Destination
——–   ———–
bin         C:\Bin
cf          C:\Program Files\Common Files
clr         C:\Windows\Microsoft.NET\Framework64\v2.0.50727\
doc         C:\Users\Keith\Documents
gac         C:\Windows\Assembly\GAC
pf          C:\Program Files
sys         C:\Windows\System32
t           C:\Users\Keith\AppData\Local\Temp
tfs         C:\Tfs
win         C:\Windows
www         C:\inetpub\wwwroot

To set-location to the wwwroot dir execute the following:

PS C:\> g www

It’s a trivial function but if you spend a lot of time at the PowerShell prompt, it (or something similar) is quite handy.

This entry was posted in PowerShell. Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s