Skip to main content

How to Read Help Files (Part 3 of 7)

Syntax is very important when writing code. A computer needs exact instructions.  That is why every computer language has a syntax.  The syntax is a set of rules that you must follow in order for a command to execute successfully.  Today we are going to examine the syntax section of a PowerShell Help File.  Execute this command:

Get-Help Stop-Service

Below is the Syntax section.
SYNTAX
    Stop-Service [-InputObject] [-Exclude []]
    [-Force] [-Include []] [-InformationAction {SilentlyContinue |
    Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
    []] [-NoWait] [-PassThru] [-Confirm] [-WhatIf]
    []
   
    Stop-Service [-Exclude []] [-Force] [-Include []]
    [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore
    | Suspend}] [-InformationVariable []] [-NoWait] [-PassThru]
    -DisplayName [-Confirm] [-WhatIf] []
   
    Stop-Service [-Name] [-Exclude []] [-Force] [-Include
    []] [-InformationAction {SilentlyContinue | Stop | Continue |
    Inquire | Ignore | Suspend}] [-InformationVariable []]
    [-NoWait] [-PassThru] [-Confirm] [-WhatIf] []

The Syntax block of information shows you all of the valid parameters for Stop-Service and a little bit about how to use them. Personally, I am not a fan of this cluster of information.  There is a lot going on here.  You can see that Stop-Service is listed 3 times.  That is because the author of the cmdlet created parameters that cannot be used at the same time.  Download my code for Get-RequiredParameters (http://mctexpert.blogspot.com/2015/12/finding-required-parameters-in.html). Once you load Get-RequiredParameters into memory, execute this code:

Get-RequiredParameters -Cmdlet Stop-Service | Where Required -eq $True

PS C:\> Get-RequiredParameters -Cmdlet Stop-Service | Where Required -eq $True

Parameter   Required Set       
---------   -------- ---       
InputObject true     InputObject
DisplayName true     Default   
Name        true     DisplayName

You are now looking at the parameter sets and the individual parameters that cannot be used at the same time.  Below is the syntax block of the help file once again with these 3 parameters highlighted.
SYNTAX
    Stop-Service [-InputObject] [-Exclude []]
    [-Force] [-Include []] [-InformationAction {SilentlyContinue |
    Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
    []] [-NoWait] [-PassThru] [-Confirm] [-WhatIf]
    []
   
    Stop-Service [-Exclude []] [-Force] [-Include []]
    [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore
    | Suspend}] [-InformationVariable []] [-NoWait] [-PassThru]
    -DisplayName []> [-Confirm] [-WhatIf] []
   
    Stop-Service [-Name] [-Exclude []] [-Force] [-Include
    []] [-InformationAction {SilentlyContinue | Stop | Continue |
    Inquire | Ignore | Suspend}] [-InformationVariable []]
    [-NoWait] [-PassThru] [-Confirm] [-WhatIf] []

Information is also displayed with a series of <> and [].  All of these items have the name of a parameter (starts with a dash character “ – “) and then is followed by its data type.  There are four common combinations of how to display this information utilizing the <> and [].  They all mean different things.

[-ParameterName]

Square braces surrounding the parameter name and then followed by the data type are required parameters.  If you omit these parameters, PowerShell will suspend execution and ask you for the value to give this parameter.

[-ParameterName ]

Square braces surrounding both the parameter name and data type is an optional parameter.  This means that you use it only if you need to.

[-Parameter Name]

A parameter name in square braces with no data type is a switch parameter.  A switch parameter does not accept an argument.  You use this to turn on extra functionality of the cmdlet.

[-ParameterName {Value1 | Value2 | Value 3}]

A parameter name inside of square braces with a set of potential values inside curly braces is an optional parameter.  The valid values for that parameter is listed. Inside of the curly braces.


Tomorrow we will look at detailed help files. 

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.