PowerShell: Tutorial 2, “Teach a Man to Fish” or Working Out The Command You Need

A series of tutorials, or How To Get Started With PowerShell:  “It’s Awesome” edition.

The title was originally built around “Teach A Man A Cmdlet and he PowerShells for a day;  Teach a Man to Use Get-Command and he PowerShell’s for a lifetime.”  But it got more and more clumsy and convoluted so I finally decided that there really was a limit to my own cleverness.

PowerShell really is quite clever though and makes it really easy to work out the cmdlets you want to use for any task.

Get-Command is used to get, well, commands.  As long as you know a word or two of the command you want you can use it to get what you want.

PowerShell commands always have the format [VERB]-[DESCRIPTION] and common verbs are GET, SET, ADD and REMOVE.

So let’s say I wanted to get the size of one of my physical disks.  I can use wildcards in Get-Command to work out what I need;tutorial-2-picture-1

Get-PhysicalDisk looks to be the one.  And that returns all my disks;tutorial-2-picture-2

I can use wildcards again to get just the one I’m interested in too;tutorial-2-picture-3

That shows me the size of the disk.  But that only summarizes a few bits of information about the disk.  If I wanted it all I could pipe the output to Format-List (which gives us everything);tutorial-2-picture-2a

Pretty cool!

If I then wanted to work out how to make some changes on my Physical Disk I could use Get-Help to get the information on the Set-PhysicalDisk cmdlet (remembering that Set- was a common verb to make changes to an object).tutorial-2-picture-4

From that I’d probably look at a few examples;tutorial-2-picture-5

PowerShell is designed to make people coming from other systems feel at home.  A lot of commands have aliases to commands used in other shells.  If you’re familiar with DOS you can just use DOS commands;tutorial-2-picture-6a

But using Get-Alias you can work out what the “real” command is under the hood and use that;tutorial-2-picture-6

And it’ll give similar results if you’re used to other shells too;tutorial-2-picture-7

You can also use Get-Member to work out what you can do to an object too;tutorial-2-picture-8






Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: