PowerShell Invoke-Item Cmdlet

Introduction to Invoke-ItemWindows PowerShell Invoke-Item

My main use for Invoke-Item is when my PowerShell script has just created a file, and I am itching to view its formatted contents.  Another use is running executables.

Topics for the Invoke-Item Cmdlet

 ♣

Compare Invoke-Item with Get-Content

To get a feel of Invoke-Item let us compare it with Get-Content:

Invoke-Item C:\Windows\win.ini

Note 1: Invoke-Item uses notepad to open and view win.ini; this works because of the file association.

Get-Content C:\Windows\win.ini

Note 2: Get-Content displays the raw contents.

As we will see in the next example, Invoke-Item comes into its own with heavily formatted data, such as html.

Problem: How to Run an Executable with PowerShell

Preamble, WinKey +r, iexplore works fine, but how do you achieve the same result in PowerShell?

#Problem: In PowerShell this plain command does not work.
iexplore.exe

# Solution: Call for PowerShell’s Invoke-Item
Clear-Host
Invoke-Item "C:\Program Files\Internet Explorer\iexplore.exe"

Note 3: I find that Invoke-Item needs the full path.

Note 4: A better way of launching a program may be Start-Process

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Creating, Then Viewing a Html File

Suppose you have just created a web page using PowerShell’s ConvertTo-Html cmdlet.  Your next step is to see whether the output displays as you expected.  Instead of ferreting around with explorer, why not append Invoke-Item?

Scenario: You want a list of running services that you cannot stop.  Furthermore, you want to publish the file to the intranet.

# PowerShell Invoke-Command example to find services you cannot stop.
$File ="D:\Intranet\RunningServices.html"
Get-Service | Where {$_.Status -eq "Running" -And $_.CanStop -eq $false} |
ConvertTo-Html -Property Name, Status, CanStop | Out-File $File
Invoke-Item $File

Note 5: What makes this Invoke-Item task easy is the use of the $File variable.

Another Invoke-Item Example

In this example I employ Invoke-Item solely to check the xml file has been created as expected.

$File = "D:\PShell\ProcUnique.xml"
Get-Process -Unique | Export-Clixml $File
Invoke-Item $File

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Research Invoke-Item Parameters

# Employ Get-Help to investigate the syntax of Invoke-Item
Get-Help Invoke-Item -Full

Note 6: Help reveals that Invoke could work with wildcards in the path, however, I rarely use it to open multiple files, for example:
Invoke-Item $env:SystemRoot\*.ini

Where Next: Check Other Members of the Invoke Family

Invoke is a rarely used verb in general English, and is not that common in PowerShell, let me introduce to the rest of this invoke family:

# List members of PowerShell’s Invoke family of cmdlets
Get-Command -Verb invoke

Invoke-Command
Invoke-Expression
Invoke-History
Invoke-Item
Invoke-WebRequest
Invoke-WmiMethod
Invoke-WSManAction

Other examples of Invoke Item

Summary of PowerShell’s Invoke-Item

My main task for Invoke-Item are when my script has just created a file, and I wish to view its contents.  I find that appending this cmdlet to a script saves me having to open explorer.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tasks:

PowerShell Home   • Shell Application   • New-Object   • PowerShell Add Printer   • PowerShell -com

PowerShell Logon Script  • Map Network Drive  • PowerShell Create Shortcut  • Free CSV Import Tool

Invoke-Expression   • Invoke-Command   • Invoke-Item   • PowerShell Expression v Command Mode

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.