PowerShell Invoke-Expression

Introduction to PowerShell’s Invoke-Expression

Invoke-Expression is a brilliant PowerShell cmdlet, which mimics us typing instructions into a ‘DOS box’.  A typical scenario is where you are expert with cmd.exe, and now you want to execute the same commands, but using PowerShell.  This is a job for the Invoke family of cmdlets, specifically Invoke-Expression.

Topics for Invoke-Expression Cmdlet

I have two ‘Vehicles’ for you to test Invoke-Expression, firstly, WOL Wake-on-Lan, and secondly Test-Connection.

 ♣

WOL – A Scenario for Employing Invoke-Expression

I have chosen Wake-on-Lan (WOL) as a vehicle to show how to combine PowerShell and cmd.  Thanks to the use of variables, it is easy to modify the Invoke-Expression instructions to run your particular command-line program instead of wolcmd.exe.

Invoke-Expression Example

While PowerShell has aliases for dir (List-ChildItem) and cd (Set-Location), there is no direct equivalent for cmd.exe.  However, the Invoke-Expression cmdlet allows you to launch an executable and crucially, append command-line instructions.  The result is that Invoke-Expression executes the string as if you had typed it in the ‘DOS box’.

How to Bring the Command-Line into PowerShell – WOL Example

PowerShell Invoke-Expression CMD Command-line

Take the scenario where you want to mimic cmd.exe, see screenshot above, my idea is to execute Wake-On-Lan with a PowerShell script containing two variables.   Firstly, control the path to the executable, secondly, append the command-line instructions that you want to pass to that program.

Summary of WOL Command
In DOS: Wolcmd 00248C1F9023 192.168.1.66 255.255.255.0 7

In PowerShell: Invoke-Expression Wolcmd.exe 00248C1F9023 192.168.1.66 255.255.255.0 7

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Example PowerShell Script Using Invoke-Expression to Wake-on-Lan

Vital preliminary step: download wolcmd.  I strongly recommend that you ‘play’ with wolcmd in the DOS box before you use it in a PowerShell script.

In the script below I have added two variables because I wanted to be sure that we control the path to the executable, and also I wanted to direct you to making your own version of my example.

# PowerShell Wake-on-Lan command-line instruction
$ExePath = "E:\Downloads\Applications\wolgui\wolcmd.exe"
$CLine = "00248C1F9023 192.168.1.66 255.255.255.0 7"
Invoke-Expression "$ExePath $CLine" | Out-Null

Note 1: You probably want to change the values of $ExePath and of $CLine

Note 2: Observe the double quotes surrounding both variables.  "$ExePath $CLine"

Note 3: Experiment with and without | Out-Null.  This command merely suppresses any output.

Troubleshooting

If you are following my actual PowerShell Wake-on-Lan example, then download wolcmd here.

If you have changed the values of $ExePath and $CLine, but your script still does not work try a manual walk-through in the cmd dos box.  See if you can get ipconfig to work with the command-line switch /all.

Invoke-Expression Using a String in a PS1 File

My idea in this PowerShell Invoke-Expression example is to store the string values in a text file, and then call that file and run those instructions.  The scenario is that you wish to measure the server’s response time with PowerShell’s Test-Connection, this is the equivalent of ping in cmd.exe.

Concept 1: My instructions ping a website using PowerShell’s Test-Connection cmdlet
Concept 2: These instructions are saved in a text file with a .ps1 file extension.
Concept 3: Invoke-Expression then ‘calls’ this text file with its PowerShell instructions.

Save this into a file, let us call it C:\PingWeb.ps1

# PowerShell Test-Connection .ps1 file
Clear-host
$WebPing = Test-Connection www.computerperformance.co.uk -count 10
Write-host Average ($WebPing | measure-Object ResponseTime -average).Average

Now let us use Invoke-Expression to execute these instructions.

# PowerShell Invoke-Expression example from .ps1 file
$Path = "C:\PingWeb.ps1"
Invoke-Expression "$Path"

As with many of my scripts, this example contains extra code which aids learning, and directs you to making changes to suit your circumstances.  However, you could simplify the expression to:
Invoke-Expression "C:\PingWeb.ps1"

See more on PowerShell Measure-Command ยป

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

Research Invoke-Expression

You can research Invoke-Expression with these two trusty PowerShell commands
Get-Help Invoke-Expression

You can also append Get-Member thus:

# PowerShell WOL – list Methods for Invoke-Expression
$ExePath = "E:\Downloads\Applications\wolgui\wolcmd.exe"
$CLine = "00248C1F9023 192.168.1.66 255.255.255.0 7"
Invoke-Expression "$ExePath $CLine" | Get-Member

More Members of the Invoke Family

# PowerShell script to list Invoke family of cmdlets
Get-Command -verb invoke

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

The two most promising cmdlets are Invoke-Command and Invoke-Item.

Problem with Invoke-Expression -ComputerName …. -command "xyz"

If it’s any consolation commands like this did not work for me:
Invoke-Expression -ComputerName localhost, BigServer -command "Get-process powershell*"

What I did was switch to Invoke-Command thus:

Invoke-Command -ComputerName bigserver -scriptblock {Get-process powershell}

Summary of PowerShell’s Invoke-Expression

Invoke-Expression is a wonderful way of executing a string of DOS instructions, but in PowerShell.  I chose Wake-on-lan (WOL) as a vehicle to give you a grounding in this cmdlet’s syntax.  The ideas is that Invoke-Expression executes the string as if you typed it at the cmd.exe command-line.  I hope that you will be able to modify my examples to suit your task.

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.