PowerShell Basics - Command Structure

Author by Mitchell Grande

Although you've almost certainly used PowerShell by now, you may not be comfortable writing your own commands and scripts.  With PowerShell being the de facto scripting platform for Windows administration, being able to confidently write and execute your own scripts is an invaluable skill.  As networks get larger and larger, the need to increase efficiency is growing, and PowerShell will help get you there.  This is the first in a short series of tech tips to help get you up to speed and confident with PowerShell.

Command Names

The first thing to understand about PowerShell is how the language is structured.  The commands that you use are often referred to as cmdlets, and they have a common format of Verb-ModuleNoun.  Although not every command follows this pattern, nearly all of them do.  The consistent format makes it possible to predict the name of commands, even if you don't remember them offhand.  Let's break down each of the parts:

The Verb part of the command is what action to perform.  The most common verbs are:

  • Get - Get the details of something without making changes to it
  • Set - Change the settings of something that already exists
  • New - Create a brand new item
  • Remove - Delete an existing item

There are many other verbs available for different purposes, but these 4 make up most of the common uses.

The Module part of the command refers to which module contains the command.  Not all commands are part of modules so they may omit this part.  For example, many of the core commands like Remove-Item, Import-Csv, and Format-Table don't include a module in their name.  Some commands that do include their module name are Set-NetIPAddress, Get-DnsClientServerAddress, and Get-BitLockerVolume.

Finally, the Noun portion of the command refers to what kind of item you are working on.  These are widely variable, but some common examples are in these commands: Export-Csv, New-AzureRmVM, and New-Item

Putting the three parts together, you can make an educated guess of what command you need for a given situation.  For example, if you you need to create an AD user account, you may know that the verb is New, the module is AD, and the noun is User.  Combining those into a command, we get New-ADUser, which is the correct way to create a new user account in AD with PowerShell.  It takes some experience to master this, but once you do, you don't have to constantly visit the manual to find the commands you need.

Object Structure

Unlike the older cmd commands, PowerShell commands use objects for retrieving and returning data.  For example, if you run ipconfig in a command prompt, you get a simple, text printout of the IP configuration of the computer.  This has the same flexibility as printed text on paper.  You can't easily control what adapters are listed, what order they're in, or which properties are displayed.  On the other hand, by running Get-NetIPAddress in PowerShell, you get an object with distinct properties that you can work with individually.  You can use these additional properties to filter results, sort them to your liking, or only include certain properties.

To help visualize the object-oriented nature of PowerShell, you can think of each PowerShell command as returning a list of key values from the Windows registry.  Imagining the regedit interface, you can see a list of name/value pairs that make up each key.  A PowerShell object is like a key folder in the registry, made up of individual values with different names.  Although PowerShell may print out to plain text on the screen, the data behind it is a rich set of values.

To see all of the values available within an object, you can use the Format-List * command.  This will print out every property and value for the given object so you can discover what an object is made up of.  For example, here we can see that there are a large number of details available when using the Get-Item command to query information about a file:

PowerShell-1-1.png

If we were writing a more complex command, we might need to know the values stored within the item to get the data we need.

Stay tuned for future content in this series!  We’ll continue expanding on the basic concepts to help make you confident in using PowerShell.  In the meantime, the PowerShell documentation has some great getting started information.
 

Author

Mitchell Grande

Systems Engineer

Tags in this Article