Prof. Powershell

Invoke-Sesame

Use the Invoke-Item cmdlet to open files and their application types -- much like Windows does when double-clicking a file.

In Windows, we are very accustomed to clicking on a file to open it. This works because there is usually an association between a file type and an application. In Windows PowerShell we can achieve a similar result. We merely need to know the magic words. No, "Open Sesame" isn't quite it. The secret word is Invoke-Item.

The Invoke-Item cmdlet needs a path to some file you would like to "invoke".

PS C:\> invoke-item c:\work\myfile.txt

This command will invoke myfile.txt and launch the associated application. In my case, that would be Notepad. If you attempt to invoke an item without an associated application, Windows will prompt you. This behavior is no different than double-clicking a file in Windows Explorer that is lacking an associated application.

The cmdlet has an alias of ii and can accept pipeline input.

PS C:\work> dir *.txt | ii

How's this for efficiency? With a very simple command I got all text files in C:\Work and opened them in Notepad. Although I could also have used this expression:

PS C:\work> ii *.txt

A better pipeline example might be that you have a list of files, they could all be different types, but you want to open them as a group.

PS C:\work> get-content c:\files.txt | ii

As long as the pipelined input to Invoke-Item is a path, you should have no problems. You can also use Invoke-Item to launch scripts and executables.

PS C:\work> $c="c:\windows\system32\calc.exe"
PS C:\work> ii $c

In this regard, the cmdlet is no different than Invoke-Expression. Although from my limited testing it appears that Invoke-Expression is a little faster.

If you want to jump out of the shell, Invoke-Expression can help there as well. Specify a folder or path and the cmdlet will open that folder in Windows Explorer

PS C:\> ii $env:temp

Boom. Just like that the %TEMP% folder is opened.

This is such a handy cmdlet I don't know why I'm not using it more. I can quickly open files without having to move my hands off the keyboard until I really need to. Need to open and edit a CSV?

PS C:\work> ii services.csv

This will launch Microsoft Excel and load the CSV file. This takes a matter seconds, including typing. I didn't have to navigate to the folder in Windows Explorer, find the file and double-click it. Or find Excel on my start menu, navigate to the CSV file and open it. Those tasks are glacial compared to using Invoke-Item. Even the alias, ii, is streamlined.

So keep your hands on the keyboard, and remember the magic words that will open up an exciting new world.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events