DiscoPosse – Using the chicken to measure IT
Technology, Cycling, Music and Madness


Technology

August 8, 2012

Get-Started: ii Captain! Using the Invoke-Item CmdLet

More articles by »
Written by: Eric
Tags: , , , , ,

A nifty little PowerShell CmdLet to make use of is Invoke-Item, which also has the alias ii. This CmdLet can be used against files, URLs and other documents and items to invoke the default action against them.

An example where we would want to use this is where we generate some output into a file such as a .txt file, and then we want it to be rendered to the screen using the editor for the .txt extension which is most likely Notepad.exe on your system.

In this case, let’s use a simple one-liner to output the path variable ($env:path) to a text file.

$env:path | Out-File MyPath.txt

Now that we have the file created, we simply run the Invoke-Item against the file to launch it using the default Open action based on the file type association.

Invoke-Item .\MyPath.txt

The result of the Invoke-Item command is that you will see a Notepad windows launch (or whatever your associated application happens to be).

This was a very simple example, but the concept is identical for any registered file type in your system. When running as an interactive script, a very popular purpose for the Invoke-Item CmdLet is to launch Internet Explorer to view an HTML file which was created by some other process.

While this is a handy little tool in our PowerShell toolbox, you may find that you want to move to the Invoke-Expression if you require more complex command lines and parameters to be available. I’ll be putting together a post on that CmdLet in the future which will go into much further detail.

The CmdLet supports relative and literal path references and requires double-quotes for long file and path names where spaces are in the path.

The Get-Help for the Invoke-Item is as follows:

The full TechNet help for the Invoke-Item can be found here: http://technet.microsoft.com/en-us/library/hh849794.aspx

Happy invoking!



About the Author

Eric





 
 

 
featured_powershell

Updating (same as parent folder) records with DNSCMD and PowerShell

In an earlier post on the site (Microsoft DNS record updates using PowerShell and DNSCMD) I noted how PowerShell cannot natively update records in MS DNS, however we could leverage the DNSCMD command and pass parameters using a...
by Eric
1

 
 
featured_powershell

CSV, yeah you know me! – PowerShell and the Import-Csv CmdLet – Part 3

This was a long overdue post, so thanks for sticking with me while I finally got back on track with our CSV, yeah you know me series (Here are Part 1 and Part 2). As I’d mentioned in the closing of Part 2, we want to be a...
by Eric
0

 
 
featured_powershell

Finding RDP sessions on servers using PowerShell

Have you ever needed to use RDP to get to a server console for some local admin work and then been bounced out because there are already active sessions? Or have you had your Active Directory account locked out because of an op...
by Eric
8

 

 
featured_powershell

It’s all about Progress: Using the PowerShell Write-Progress CmdLet

If you are like me and you like to know how your task is going in a PowerShell process, this is a great little tip for you. I’ve got a number of long running scripts that perform actions against a collection or query. The...
by Eric
2

 
 
alias-logo

A.K.A. – Using PowerShell Aliases

An interesting capability baked into the PowerShell environment is alias commands. Much like we can find in IOS (The Cisco one that is) and with other CLI environments, there are short commands available to save you some typing...
by Eric
0

 

 
powershell-for-developers-cover

DiscoPosse Review: Windows PowerShell for Developers by Doug Finke – O’Reilly Media

For those fans of PowerShell, and for those just getting on-board, PowerShell MVP Doug Finke has built the bridge for your journey with his latest book Windows PowerShell for Developers from O’Reilly Media. As a long time...
by Eric
0

 



Join Zipcar and get $50 in free driving Join Zipcar and get $50 in free driving Join Zipcar and get $50 in free driving

0 Comments


Be the first to comment!


Leave a Reply

Your email address will not be published. Required fields are marked *

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