Windows PowerShell's Syntax

Introduction to PowerShell's Syntax

The fact that you
almost don't need this page is a testament to the intuitive nature of PowerShell. Yet for those who wish to save time fumbling with the
PowerShell syntax, it may pay to have a refresher of these rules of
scripting grammar.

For many years a bad attitude to syntax hindered me. My
breakthrough was realizing that punctuation marks are there to aid the
readers' understanding; my mistake was thinking syntax rules were designed
by my English teacher as a way of finding new ways to tell me off.

With PowerShell's syntax the comma is frequently used to separate items on a list.
Whereas the semi-colon is used to split separate ideas. Let us study this
example:

The equals sign (=) behaves just as expected. As usual,
'=' tests for equivalence, my main use for equals sign is to sets a variable to
= a certain value.
The equals sign has a counterpart ! (Exclamation
mark) meaning, 'not equal'. You may also employ -Not instead of ! I just include these
two basic operators, '=' and ! for completeness.

PowerShell -eq

PowerShell has a family of conditional operators

-eq meaning equals

-ne in the negative, not equal to...Note: there is no
-neq operator; just use the two letters -ne.

-gt and also -ge (greater than or equal)

-lt and also -le (less than or equal)

Here is how you would use the most famous member -eq

Clear-hostGet-Service | Where-Object {$_.Status -eq
"Running"}

Note 4: Don't be tempted to use the "=" sign here, that
would be a big mistake.

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 fundamentally case insensitive.
Every object and every cmdlet is case insensitive. Set-Location
performs exactly the same action as set-location. However, where your data has case sensitive values, there are PowerShell operators to deal with
'case'. For example, -gt means greater than, -Match means
contains a particular string value.

However, you can force these and similar operators to be case sensitive by prefixing
hem with a 'C'. -CMatch, or -CGt mean that the comparison will be case sensitive.

When I wanted to join text and numbers, I spent time looking for PowerShell's concatenator. Silly me, all
I need is the simple + plus sign. Where other languages use + for
adding numbers, PowerShell uses ' + ' for
joining strings, or even for combining text with numbers:

Some people call this symbol (-) minus, others a refer to this sign as a dash, I mostly call it a hyphen. Let me be clear, this character maps to ASCII 45, to see the character, hold down ALT key, type 45 on numeric keypad, now let go of ALT key.

PowerShell uses this - symbol for two
purposes. Firstly, to join Verb-Noun pairs, for example Out-File guy.txt. Secondly, this minus sign is also used for
parameters, modifiers, or filters such as -List; as in Get-Eventlog -List. The
trap I fall into is to put a space between the minus and the modifier. get -Eventlog is clearly wrong,
because there is a space between get and -. The correct format is, Get-Eventlog,
with no space.

Guy
Recommends: Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft's operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory, or Exchange Server. Give this WMI monitor a
try - it's free.

The ability to pipe the output of one command, so that it becomes the input
of the second command is PowerShell's signature tune. Thus it is
important to be clear about this | symbol.

When typed in notepad, the pipeline symbol looks like this: | but when typed in the Microsoft Shell
it looks like ¦. On my keyboard the key I am using this symbol is next to the z, however I have seen keyboards where the
pipeline key is next
to numeric 1 on the top row. Once you find, then type the key, you get a pipe symbol
(|).

To be crystal clear this pipeline symbol corresponds to ASCII 124. N.B this not ASCI 0166.
Test by holding down the Alt key and typing the number (124 or 0166) on the numeric pad, then letting go of the Alt key.

In PowerShell syntax the pipeline symbol (|) has three roles.

Think of the pipeline as a method for joining two commands. Get-Eventlog system |
Format-ListYou could even have two pipelines in one statement.

If I had to choose one element of PowerShell's syntax to master it would
be the bracket. I love the logic of an 'If' statement; however, to get
the command to work you have to understand If (parenthesis for condition)
{curly brackets for payload}.

At first PowerShell's brackets surprised me. Each type has a
specific role, the wrong bracket will cause an otherwise sound command, to
fail miserably. The message is clear, you have to understand your
brackets. Let us see how each of these (), {} or [] has a different
purpose.

1) () Parenthesis or Curved brackets are
used for required options in the foreach loop