Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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: A Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

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

Wake-On-LAN really will save you that long walk to awaken a hibernating machine; however my reason for encouraging you to download this utility is just because it's so much fun sending those 'Magic Packets'.  As Wake-On-LAN (WOL) is free, see if I am right, and you get a kick from arousing those sleeping machines.  WOL also has business uses for example, wakening machines so that they can have their patches applied. 

Download your free copy of 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 installed in a text file with a .ps1 file extension.
Concept 3:  Invoke-Expression then 'calls' this text file with its string 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"

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

Solarwinds' Orion 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.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is 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 take advantage of Solarwinds' offer.

Download a free trial of the 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-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 tutorials:

PowerShell Home   •Shell Application  • New-Object   • PowerShell create shortcut

PowerShell Logon Script   • Map Network Drive   • PowerShell add printer   • PowerShell -com

Invoke-Expression   • Invoke-Command 

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.