PowerShell Tutorial – CmdLets

In common with other scripting languages, in PowerShell   has basic language elements like Variables, Arrays, Functions, Objects, Loops, IF statements, Switch statements, etc. (See here for a full explanation)

PowerShell CmdLets (Commands)

In PowerShell  commands are known as CmdLets (pronounced Command Lets). Cmdlets follow the naming convention of Verb-Noun combination:

E.g: Get-Help, Get-Service, Get-Process

PowerShell cmdlets are not case sensitive and are the smallest unit of functionality in PS.  You can use either the PowerShell Integrated Script Environment shown below (just type powershell_ise into the search box in the Windows start menu to launch this) or the command line tool to execute Cmdlets.

PowerShell Tutorial

In the PowerShell ISE. you enter the command at the prompt and the results of the cmdlet will be displayed in the middle panel of the tool. You can use the top panel in the tool to write scripts and also to execute single Cmdlets. The F5 key executes the entire script while F8 key executes only highlighted Cmdlets (Run Selection).

PowerShell Tutorial

Cmdlets are simple to type, you can use Tab key to auto complete the Cmdlet. E.g: Type Get-Pro then press the Tab key.

Cmdlet parameters

Cmdlet accept  which are denoted by using the “-“symbol:

PowerShell Tutorial

For example, the “-Name” is the parameter instructs PowerShell to display only “winrm” service information. Parameters are  also  auto completed  using  the Tab key, so that you don’t have to remember the entire parameter name.

Piping

Piping or pipelining is a method of combining two or more PowerShell Cmdlets to do a single task. PowerShell is a fully objected oriented scripting language as a result Cmdlet   returns an object as result. To combine Powershell Cmdlets you can use the symbol, “|”.

PowerShell Tutorial

The Get-Service cmdlet returns all services (whatever the state) on the local machine. It returns as an object. The “|” or piping passes that object in to next cmdlet (where-object) which essentially does the filtering.  The braces “{ }” represents the body of the where-object cmdlet, whichs specifies a condition. “$_.Status” is the current object (“$_.”), property (“Status”) and “-eq” is the logical condition (ie “=”).
Operators   in PowerShell user characters and not as symbols as  in other languages.

The Where-object cmdlet iterates through all the objects returned from the Get-Service cmdlet and filters out only the objects which are have the status of “Running”.Continues…

One thought on “PowerShell Tutorial – CmdLets

  1. Pingback: PowerShell for Sharepoint – Cmdlets | Windows Server Help

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>