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.

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

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.

Guy Recommends: SolarWinds Free Wake-On-LAN Utility

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.

Troubleshooting

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.

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 cmdletConcept 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.

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"

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.

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.