/* ]]> */
Aug 232011
 

…or stated differently: How To create a PowerShell Alias that can be used to launch a Windows program from the PowerShell command prompt. :-P

I often find it convenient to run various programs directly from the PowerShell command line. In particular, I often need to run a text editor or Microsoft Excel. Using alias short-cuts, I can conveniently work with text files (including powerhshell scripts) and csv files in that are located in (or close by) my powershell directory without navigating to the Windows GUI.

I could add these programs to the PATH environment variable which would work fine but I find it to be a lot more convenient to setup an ALIAS. Using an alias  allows me to shorten the program’s name; e.g. I use “npp” for “notepad++.exe”. That’s easier and faster which is the whole point of working with powershell in the first place.

The first thing that came to mind when developing this script is that I wanted to run it on all of my various Windows hosts. One small problem was that the program for which I’m creating a shortcut (alias) might not be present or it might not be in the same directory on each machine. For example, my Server 2008 R2 host keeps the 32-bit programs in the “c:\program files (x86)\” directory. Also, I wanted to make sure that I didn’t stomp on any existing aliases. Both of those situations are handled gracefully in the script, complete with user feedback via the console so you know exactly what alias has (or has not) been set.

Here is pseudo code for the process:

  • define a parse-able array containing shortcut (alias) names and a delimited list of possible paths (sample code lines 1 thru 4)
  • for each array element :
    • parse-out the first element to use as an alias
    • if alias is not in use :
      • split the second array element into program path(s)
      • for each program path :
        • if valid path, set the alias and break out of loop
      • end for
    • end if
  • end for

Information is displayed to the console to let you know what’s happening

Sample Screenshot:

Screenshot from Setup-Shortcuts.ps1 script

Screenshot

 

I have taken the contents of the following script and incorporated into my $profile startup script. If you need to add to (or change) the list of shortcut items, modify the $PgmAliasList variable using the following format:

$PgmAliasList = (“alias | path1[;path2[;path3...]]”[, ...repeat as needed for more aliases...]

Hopefully this makes sense… each array element consists of a parse-able string that has an alias name followed by a vertical bar; after the vertical bar you can list one or more program paths separated by a semi-colon. If the alias is not in use, we will assign the first valid path found in the list of paths. The reason for having multiple paths is so that this single script could be incorporated into PowerShell profile script and run from various hosts where program locations might be different.

 Posted by at 12:17 pm

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Page optimized by WP Minify WordPress Plugin