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.

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 + 1PS > 1 + 1

# More productivelyGet-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).

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.

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 + 1write-Host " $Num "

# ResultPS> 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 + 1write-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.