Calling Powershell scripts from Launchy

Powershell is very cool, but I have had a helluva time calling powershell scripts directly from Launchy. If you would like to know how I finally figured out how to nail down this capability, read on.

The Problem

The problem is not Launchy, but that it can be very painful to execute a powershell script unless you are already within a powershell console. Because Launchy runs scripts from the shell it resides in, the same problems occur within it that occurs when you are running from the command line. So in order get things integrated between Launchy and powershell, we have to get powershell scripts running from the command line.

Examples

Let’s start with a really simple script that we want to eventually call from Launchy with a parameter. This script just opens notepad, taking a parameter to identify a filename to open. Not too complicated, but we are not focused on the script itself, just how to get it running from Launchy.

open-notepad.ps1
Invoke-Item "C:\WINDOWS\system32\notepad.exe" %1

Our goal is to be able to do this…

Launchy opening a Powershell script with parameter

… and have notepad attempt to open “C:\temp.txt”. Of course, this is assuming that we have set up Launchy’s catalog to include powershell ps1 scripts in the directory our open-notepad.ps1 script resides.

If you tried this Launchy execution at this point, it would probably attempt to open the script in a text editor, because the *.ps1 file type default application is not powershell. And even if you set powershell as the default application, you’ll still get some error like this:

The term ‘C:\Documents’ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and
try again.
At line:1 char:13
+ C:\Documents <<<< and Settings\matayl\scripts\open-notepad.ps1

You cannot execute this script directly from the command line (as noted in this helpful article), because you need to use an absolute path:

In general, you need to type the complete file path in order to run a script. That’s true regardless of your location within the file system.

You might also have a problem if running powershell scripts is disabled on your system entirely (read the article for more info on this, too).

If you were to open a powershell console, you would be able to figure out how to execute this script without much problem. But we need to find a way to run a powershell directly from the cmd console in order to allow Launchy to run it.

After much searching, I found a blog post on MSDN that ultimately allowed me to accomplish my goal. In this post, the author shows how to embed powershell commands directly into a CMD script! Although this is a roundabout solution, costuming my powershell scripts as CMD scripts is better then not being able to run powershell scripts from launchy.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted November 9, 2008 at 7:03 am | Permalink

    This post has been sitting for almost a year, and I would rather publish it in its current state than just delete it. I no longer use Windows, PowerShell, or Launchy anymore, but maybe this will help someone a little bit.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
Check out the latest GroovyMag to see an interview with me about the 1.1 release of GrailsUI: