Windows PowerShell – Expression Mode or Command Mode?

Introduction to Windows PowerShell – Expression Mode or Command Mode?

Do you care if the T.V. programme you are watching is live or recorded?  If it’s a soap episode, probably not; but if it’s a news or a sports programme then you want live action.  The situation with PowerShell is comparable;  ‘Do you really care whether it’s in command mode or expression mode?’  I believe that the answer is mostly, ‘no’.  However, when you are troubleshooting, then it is worth understanding the difference between expression and command mode.

Topics for Expression Mode or Command Mode?

 ♣

1) PowerShell in Expression Mode

1 is clearly a number (one).  Thus PowerShell switches automatically to Expression mode:

1 + 1
# PS > 2

# You can also try:
3 * 4
# or
27 / 9

Note 1: I think of Expression mode as mathematical mode, or PowerShell as a pure calculator.

2) PowerShell in Command Mode

In command mode, instructions are treated as strings unless they are obviously numbers.  The line begins with ‘Write-Host’, which is a command and not a number, hence PowerShell silently switches to command mode and treats 1 + 1 as a text string:

Write-Host 1 + 1
PS > 1 + 1

# More productively
Get-Service
Set-Location c:\windows

Note 2:  PowerShell switches to command mode as soon as it sees that the first character is not a number.  While you could ask PowerShell to just reflect text, the whole point of scripting is to manipulate, extract or change information, and for this PowerShell has hundreds of built-in cmdlets, for example Get-Process, or Stop-Service SNMP.

3) Controlling Command Mode with Parentheses

Write-Host (1 + 1)
PS > 2

Note 3:  Throughout PowerShell you need to chose your style of brackets carefully.  In the above example {Braces} have a different effect compared with (Parentheses).

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

4) Controlling Expression and Command Mode with Speech Marks

PowerShell is able switch seamlessly between expression and command mode.  Write-Host is a command, while $Num is a variable holding a simple calculation.

Clear-Host
$Num = 1 + 1
write-Host " $Num "

# Result
PS> 2

Note 4:  The double quotes are superfluous, you can leave them out and achieve the same result.  However, were you to use single quotes then they would suppress the expression mode and ‘ $Num ‘ would be treated as a literal.

Clear-Host
$Num = 1 + 1
write-Host ‘ $Num ‘

# Result
PS> $Num

Note 5:  Actually this example is much about the differences between single and double speech marks as it is about comparing expression and command mode.

Conclusion of PowerShell Expression and Command Mode

I am not sure if I am more surprised, when PowerShell can automatically adds plain numbers, or when it manages to process commands with the minimum of syntactic clues.  After a while you take PowerShell’s built-in cleverness for granted, the problem comes when you over-step the line and take liberties.

My parting shot is that you get the desired effect relatively easily, just employ the appropriate brackets or speech marks.

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.