Home Datacenter 5 Quick PowerShell Tricks To Help You Out

5 Quick PowerShell Tricks To Help You Out

by Philip Sellers

For 2018, I’m hoping to do a weekly PowerShell related post based on some of my experiences.  I have found that a lot of my infrastructure-focused peers don’t have a great programming background and my hope is that these posts can help folks feel more comfortable conquering their work in PowerShell and save a lot of time!

For the first post, I’m sharing 5 basic things that can help you do more with PowerShell, faster.

Open Get-Help in a window

Everyone should know to use Get-Help.  It is the equivalent to a Linux MAN page within Powershell and it tells you all about a cmdlet or lists available cmdlets.  Wouldn’t you like to have that information while you’re writing a command without scrolling?  Try adding -ShowWindow to the Get-Help command.  This will put the output into a window, with resizing, search and full details.

Get-Help Get-ADUser -ShowWindow

Get_help

Investigate data and methods in an object

The Get-Member cmdlet, also abbreviated GM, will enumerate the properties and methods inside of an object in PowerShell.  This will let you know what methods you can run directly on the object and what properties you can select, scope or sort with.   Methods are things that you can do and many times they return data.  The data type of the returned data is in the Definition.  Properties are pieces of data and the Definition shows the type of data.

Parenthesis to embed a cmdlet in a cmdlet

This is one of my go-to problem solvers, especially in PowerCLI.  If you have a cmdlet that requires parameters that need to be other objects, you can always embed a cmdlet in that position enclosed with parenthesis.  PowerShell will execute the cmdlet in the parenthesis, return the object and pass it in as a parameter.  This isn’t the pipeline.  Here’s a PowerCLI example – If you need to move a VM with Move-VM you may want to specify a datastore.  To find a datastore, you can use Get-Datastore or Get-DatastoreCluster.

Move-VM -VM vmname -Datastore (Get-Datastore DatastoreName)

Respect the pipline & use it to move stuff

PowerShell is object oriented, meaning all data returns is stored in an object.  The object has both the individual data fields with values and it has methods that can be used to change or update the data.  PowerShell has the concept of the pipeline.   The pipeline allows you to move an object from command to command while doing work with the object.

For instance, you have a Get-ADUser cmdlet that returns users from AD.  There are 10 users returned from your result and they come back as an object displayed on screen.  If you use the pipe character – | – you can take this object and pass it into the next command, the way a pipeline passes water, fuel or oil from segment to segment.

The easiest way to start with commands is to use similar ones – Get-AdUser and Set-ADUser work well together to make changes to user accounts.  In the example below, we set the Company name for users using Set-ADUser.

Get-ADUser -Filter * | Set-ADUser –company “mycompany”

When you can’t Pipe it, Loop it with ForEach

Sometimes you can’t Pipe an object into another cmdlet.   In those cases, don’t forget you can loop in PowerShell.  Loops are very useful and my favorite is the ForEach.  A ForEach loop will accept the object via pipeline from another cmdlet and then iterate through every object to let you do work.  Inside of the loop, the $_ represents the current object and you can use dot notation to get access to other commands.  Everything in the loop is enclosed in a set of curly braces { }.

You will commonly use this method when working between dislike cmdlets – like getting a list of computers from PowerCLI (VMware’s PowerShell Toolkit) and then doing work on them using AD cmdlets from the built-in toolset.

Get-VM | ForEach { Do some work here }

You may also like