Windows PowerShell


PowerShell's Top Ten Parameters (Switches)

PowerShell's Top Ten Parameters, or -Switches

Most of PowerShell's commands can be fine-tuned with -parameters, otherwise known as switches.  My aim of this page is to encourage you keep a notebook of such useful PowerShell parameters.  To begin with, let me explain the basics with an example; here is a command which searches just the top level container:
get-Childitem c: \windows

Now when you add the -recurse parameter, the command takes longer to execute, but it's well worth the time to drill down the directory tree, and find the file that you are seeking in a sub-directory:
get-Childitem c: \windows -recurse

Topics for PowerShell's Parameters

 ♣

Guy's Top Ten PowerShell -Parameters

  1. -list with get-WmiObject (get-Eventlog -list)
  2. -auto (Adjust the width with format-Table)
  3. -MemberType (get-Member)
  4. -recurse with get-ChildItem (Sub-directories)
  5. -force with get-ChildItem (Lists hidden files)
  6. -groupBy (Collate similar items)
  7. -query (WMI Select statement)
  8. -filter (get-Wmiobject "Win32_Process")
  9. -com (new-Object)
  10. -whatif (Test before you commit)

New in PowerShell v 2.0 -online parameter

The technique is to add the parameter directly after the main command.  Remember to introduced your parameter or switch with a -minus sign, and not a backslash.  If you apply the terminology strictly, then the difference between a parameter and a switch is that a switch does not take a value, whereas a parameter can.

  ˚

How to Research More PowerShell Parameters or -Switches

If you play strategic games like chess, you may be familiar with the idea of once you have found a good move, then look for an even better tactic.  So it is with PowerShell, if you find a good command such as get-Eventlog system, look for parameter to refine the output, for example: get-Eventlog system -newest 20.

Situation: You know the cmdlet and want to investigate its parameters

The key question is how did I know about the -newest parameter, as PowerShell calls this appendage?  The answer is I called for help.  To be precise:

get-help get-Eventlog

# Or better still:

get-help -Eventlog -full

 I recommend that you spend time studying the PARAMETERS section of any cmdlet that you are working with, you are sure to unearth one unexpected gem.

Situation: You know the parameters name, but want to find the cmdlet(s)

For example, to see a list of cmdlets that support remoting try:

get-command | where { $_.parameters.keys -contains "ComputerName"}

The Concept of an Optional, or an Assumed Parameter

If you digest every nuance of what get-help says, then you discover that each Parameter has properties, e.g.:
Required?  True or False
Position: 1 or 2 etc.

Both logic and practical experience show that if a parameter is not required, and it's in position 1, then you could safely omit it.  In other words the parameter is assumed.  Let us use get-Childitem and -path as an example:

get-Childitem -path c:\windows

get-Help get-Childitem

# Results:
#    PARAMETERS
#       -path
#        Required? False
#        Position? 1

Thus get-Childitem -path c:\windows could be reduced to plain
get-Childitem c:\windows.   (or even: gci c:\windows)

Another example of an assumed parameter comes from get-Eventlog.  This command will not work unless you help PowerShell by supplying the name of the log.  The parameter is -LogFile.  For example get-Eventlog -LogFile system.  However, PowerShell understands that the first word after eventlog is the name the log, and thus we can omit the -Logfile parameter.  PowerShell assumes that the only possible parameter in this position is -LogFile and the following command completes successfully: get-Eventlog system.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Second series of useful PowerShell -Switches

  1. -path (Example of an optional or assumed parameter)
  2. -filePath (Variation on -path, used with Out-file)
  3. -replace (select-String)
  4. -pattern  (select-String)
  5. -descending (sort-Object)
  6. -value (add-Content)
  7. -newest (get-Eventlog)
  8. -last (Win32_Process pageFaults -last 5)
  9. -computerName (Useful scripts requiring loops)
  10. -computer (works equally well in most scripts)

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Pattern Matching Switches

PowerShell also has a family of comparison or pattern matching switches.  You may see these conditional operators such as -match and -like in 'Where' clauses, for example:

get-wmiobject -list | where {$_.name -match "Win32"}

This family of switches has several names, conditional operators and pattern matching switches, let me introduce the family:

  1. -match
  2. -like
  3. -contains

Sometimes logic dictates that their negative cousins produce tighter code, for example: -notmatch

Although we are straying further from my original idea of switches to modify a command, I should mention the logical operators as they too are introduced by a -dash.

  1. -eq  (Beware in PowerShell you cannot substitute an equals sign for -eq)
  2. -ne (Not equal, incidentally -neq will not work)
  3. -and
  4. -or

There are also bitwise variants -bor -band.  This is not an exhaustive list, there are exotic operators such as an exclusive or called -xor.

Summary of PowerShell's Parameters

Understanding PowerShell's parameters will improve your scripts.  Firstly, you get extra capabilities, for example -recurse, secondly you greater precision, for example -MemberType.  In conclusion, never miss a chance to research a Parameter or a Switch, if you find a really good example, email me and I will add it to Guy's top 10 Switches.

See more Microsoft PowerShell tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

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.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.