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.



Top Ten PowerShell 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.

Topics for PowerShell Parameters

 ♣

Guy's Top Ten PowerShell -Parameters

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

Benefits of 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\*

# PowerShell Parameter Example -recurse
Get-Childitem C:\windows\* -recurse

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:

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 name of 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:

Clear-Host
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) name

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

# Find a PowerShell Parameter
Get-Command | where { $_.parameters.keys -contains "ComputerName"}

Modification to display just cmdlets with a particular PowerShell parameter.

Clear-Host
Get-Command| where { $_.parameters.keys -contains "ComputerName"} `
| where {$_.commandtype -contains "cmdlet"}

Note 1: Observe the backtick `.  Also there should be no space after that `.

Simplification employing PowerShell's -and :

# Research PowerShell Cmdlet
Clear-Host
Get-Command | where { $_.parameters.keys -contains "ComputerName" `
-and $_.commandtype -contains "cmdlet"}

See more on Get-Command itself

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

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft 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.

Download your free copy of WMI Monitor

Fantastic Script to Research PowerShell Parameters

My real-life task was to research which PowerShell parameters contained -As, for example -AsHashTable.  I posted my question in the www.superuser.com forum, and this was a marvellous reply from Joey:

# Research PowerShell Parameters by Joey
Clear-Host
Get-Command | where { $_.CommandType -eq 'Cmdlet' } | ForEach { $_ | `
Add-Member -PassThru NoteProperty AsParameters ($_.Parameters.GetEnumerator() | `
Where {$_.Key -cmatch '^As([A-Z]|$)'} | ForEach { $_.Key }) } `
| Where { $_.AsParameters } | select Name, AsParameters

Note 2: The pattern I was looking for was 'As'.  Naturally, to research your parameters just substitute the text string you are interested in.

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:  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.

Abbreviating PowerShell Parameters

One source of confusion is that the same PowerShell parameter seems to have two or more forms, for example:  -ComputerName -computer or even -comp.  The explanation is that PowerShell's intelligence means that you only have to use sufficient letters to provide a unique combination that cannot be mistaken for any other parameter.

-foreground and -background example

# Abbreviating PowerShell Parameters
$Date = Get-Date
Write-Host $Date -ForeGroundColor green -b magenta
Write-Host $Date -fore yellow -back green
Write-Host $Date -f red -BackGroundColor black

The actual parameters are -ForegroundColor and -BackGroundColor, but as you can see, we can shorten them right down to -f  and -b. 

Guy's Second Series of Useful PowerShell Parameters (-Switches)

  1. -path (Example of an optional or assumed parameter)
    -filePath (Variation on -path, used with Out-file)
  2. -query (WMI Select statement)
  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) (-computer works equally well in most scripts)
  10. -useTransaction  (new in PowerShell 2.0)

New Parameter in PowerShell v 2.0 -online parameter

The technique is to add the parameter directly after the main command.  Remember to introduced your parameter 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.

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.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials   • Introduction   • Vista PowerShell   • Windows 7 PowerShell 2.0

PowerShell Parameters   • -Online    • PowerShell Alias   • 3 Key Commands 

Cmdlet scripts   • Windows PowerShell   • PowerShell $_ Variable

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.