Department 1337

They mostly code at night. Mostly.

Executing Powershell Scripts With Launchy

An app launcher is an application that lets you launch applications without taking your hands off the keyboard. Just hit the hot key, type a few characters to match the application or file you want to open and you are done. Most launchers can do a lot more than just launching applications, e.g. open websites if you type an URL, act as a simple calculator or—as we will explore in this post—execute scripts.

Invoking Powershell via Launchy

I have been using the Launchy app launcher for a few years, but not for more advanced tasks than the Windows Start button can handle. So I decided to try some more advanced uses.

Since Launchy can execute batch files, you can use it to perform almost any task you can think of.

The first thing we need to do is to tell Launchy where to look for the batch files we want to execute. You do that in the settings under the Catalog tab. Add the directory where the scripts are located and add *.cmd and *.bat as file type filters. I keep my scripts in a Dropbox folder that is also a Git repository. That way I have easy access to my scripts on any Windows computer I use.

Next we need to create a .bat or .cmd file that will implement our task. This can be just about anything. Launchy also lets you supply parameters to a command by pressing tab when you have matched the command you want to execute.

In this example I will create a batch file that executes a Powershell script that encodes its argument using Base64 encoding. We start with the simple batch file that will bootstrap the Powershell script:

Bootstrap batch file base64.cmd
1
2
@echo off
powershell.exe -file .\base64.ps1 %1

The batch file just starts a new Powershell process, passing the name of the Powershell script that implements the Base64 encoding task. The first argument of the batch file is passed on to the Powershell script.

Powershell.exe offers three different ways to pass code to be executed.

  • A script file via the -File parameter
  • Base64 encoded command via the -EncodedCommand parameter
  • A string or script block via the -Command parameter

The Powershell script first tests whether its argument is the path to an existing file. If it is a file path, the file’s content is read and put in a variable. If it is not a file, the text of the argument is encoded using UTF-8. Then the data is Base64 encoded and the result is piped to the clipboard using the clip.exe utility.

Base64 encoding script base64.ps1
1
2
3
4
5
6
7
8
9
10
param($text)

if(test-path $text) {
    $path = resolve-path $text
    $bytes = [System.IO.File]::ReadAllBytes($path)
} else {
    $bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
}

[convert]::ToBase64String($bytes) | clip

Now you can just trigger Launchy and type

base64 > c:\temp\cute_cat.jpg

and the Base64 encoded content of the file is ready for you on the clipboard.

If you are interested my scripts are available on GitHub and currently I have implemented four simple text manipulation tasks: urlencode, base64, slugify and hash.