SEARCH

How-To Geek

5 Cmdlets to Get You Started with PowerShell

image

PowerShell is quickly becoming the preferred scripting language and CLI of Power Users as well as IT Pros. It’s well worth learning a few commands to get you started, so we’ve got 5 useful cmdlets for you to learn today.

Get-Command

The Get-Command is one of the most useful cmdlets in the whole of PowerShell, as it will help you getting to grips with PowerShell by letting you search for certain cmdlets. Using Get-Command on its own is admittedly not very useful as its just going to spit out every command that PowerShell has.

image

But from this we can see that that objects that PowerShell outputs have both a Name and a ModuleName property. Using this information we can fine grain our search, by searching for cmdlets that contain certain terms. For example if I wanted to find all cmdlets that contain the word “IP”, I could do this:

Get-Command –Name *IP*

image

As you can see we still get quite a few results, our next option is to search within a specific module. In our case i will choose the NetTCPIP module.

Get-Command –Module NetTCPIP –Name *IP*

image

Get-Help

Once you have found the cmdlet you are looking for using Get-Command, you are going to want to know the syntax and how you can use that specific cmdlet. This is where Get-Help comes in, if you have ever used the command line in Windows you probably did something like this:

ipconfig /?

Well that doesn’t work in PowerShell, this is because in PowerShell a space is used to separate a command from its parameters. So instead we use Get-Help and pass a cmdlets name to Get-Help as a parameter.

Get-Help Get-Process

image

Get-Member

Get-Member allows us to get information about the objects that a cmdlets returns. The catch with get-member, is that it relies on PowerShell’s pipeline feature, to demonstrate this, we will can use the Get-Process cmdlet.

image

As you can see PowerShell’s output shows us some of the properties, which you can see at the top of each column. The first problem is that, while those are the properties you might be looking for most of the time, there are still more of them. The second problem is that it doesn’t show any methods that we are able to call on the object. To see the methods and properties we can pipe our output to Get-Member, like so:

Get-Process | Get-Member

image

While it may mean nothing to you right now, you will sooner or later need to use Get-Member, and the sooner you learn to use it the better. As an example, using the information from the output we could do something like:

Start-Process notepad.exe
$NotepadProc = Get-Process -Name notepad
$NotepadProc.WaitForExit()
Start-Process calc.exe

That script will launch notepad,  it then assigns output of “Get-Process –Name notepad” to the $NotepadProc variable, then we call the WaitForExit method on $NotepadProc which causes the script to pause  until you close notepad, once you have closed notepad then the calculator will launch.

$_(Current Pipeline Object)

While not exactly a cmdlet, it is one of the most used special variables in PowerShell. The official name for $_ is “the current pipeline object” . It is used in script blocks, filters, the process clause of functions, where-object, foreach-object and switches. However it is easier to explain with an example, which brings us to our next and final cmdlet, Where-Object.

Where-Object

Where-Object does exactly what it sounds like, it selects an object based on whether it meets a certain criteria. This will bring together $_, and the properties we can see using Get-Member. To demonstrate this, we will pipe the output of Get-Process into the Where-Object cmdlet.

Get-Process | Where-Object {$_.Name –eq “iexplore”}

image

So what’s going on here you ask? Well the first thing we are doing is getting a list of processes on our computer and passing the output (using the | character) to our Where-Object cmdlet,  which takes a script block  as a parameter. The script block (defined by the curly braces) instructs the Where-Object cmdlets to only select objects where their name parameter is equal to “iexplore”, and so we only get a list of the IE instances that are running. That’s all there is to it, have fun!

Taylor Gibb is a Microsoft MVP and all round geek, he loves everything from Windows 8 to Windows Server 2012 and even C# and PowerShell. You can also follow him on Google+

  • Published 06/18/12

Comments (8)

  1. SatoMew

    Slightly off-topic question: Why are you still using the Windows 8 Developer Preview or Consumer Preview in articles around here when the Release Preview is already available? It’s because the screenshots reveal that and the Desktop UI has suffered changes in the Release Preview and will further be altered in RTM.

  2. Frank

    gee – nice and easy to read – not …

    I’m an ex-programmer so I opened this article for a quick overview of PowerShell which I haven’t used yet – and hmmm – ‘Get-Process | Get-Member’ – ‘While it may mean nothing to you right now, you will sooner or later need to use Get-Member’ – gee – thanks for the helpful explanation … not !

  3. Justin

    an ex-programmer who doesnt know what Members are ? Bahahahahaha

  4. TheFu

    PowerShell brings the power of *nix-like scripting to Windows. Very nice.

    It must be a “best practice” to spell out those long commands – and for production scripts I can see the point – but when we’re just hacking a little script, doesn’t everyone use the short alias names?

    Get-Command = gcm

    After you open a PowerShell window, just type “alias” to see all the short names. There is good news for *nix people – PowerShell was designed by a former team of *nix guys, so many of the aliases will be familiar to us. ls, cd, pwd, pushd, popd, sort, cp, mv, rm, rmdir, mkdir, tee, …. you get the idea.

    Also, in order to make any use of PowerShell scripts, there is a security policy setting that needs to be disabled since most people won’t be “signing” scripts. What is that command? Ah – found it. http://stackoverflow.com/questions/4037939/powershell-execution-of-scripts-is-disabled-on-this-system

    Does anyone actually sign scripts?

  5. Taylor Gibb

    @TheFu i work at a HUGE software dev house and we have partners all over the world and we STILL dont sign scripts, in fact most of our execution policies are set to Unrestricted.

  6. Whitehairedgeezer

    I’m with Frank – as an overview the article does nothing for me – I’ve written fairly big programs in DOS; Basic; Pascal and Delphi and this meant absolutely nothing to me. What is the advantage of scripting over DOS anyway? (oh and Justin I dont know what Members are either – never needed to and an ‘article to ‘Get you started’ should explain the terms it uses)

  7. TimTom

    With regards to the term gp|gm is ref to OO code terminology like Object-member methods, propoerties. I see you’re a Delphi coder so I suppose if you consider it in that vein it might make a bit more sense to you.

    I must admit initially when I first looked at PS sometime ago, I couldn’t see the point of interest for me and saw it merely as something for Sys-Admins. However, now I have a very different view! One of the very powerful aspects of PS is that you can script in .NET code directly into the script as well as say SQLCMD or bring in full SSMS objects. With the release of Denali (SQL 2012) you can also access BISM (the new Self Service BI engine behind PowerPivots). The Exchange Server 2010 management UI is basically a font end to whole bunch of PS scripts.

    As with most things with tech like this, if you are versed in DOS then you’ll most probably be more productive using cmd files than learning a new tech tree or methodology. I suppose you could call it a choice thing, I used to code in VB, now I do C# and some C++ I couldn’t go back to VB now, but initially I hated being forced to move away from VB6. After the initial learning curve, you should find that PS can in some cases be more verbose than cmd scripting, but it can be a little more versatile and has a more OO code kind of syntax structure to it (great if your an OO coder!).

  8. bmadtiger

    Well this would be nice if it actually worked in the real world. I tried this out on my Win 7 SP1 PS console and didn’t get much luck. The “Get-Command” command, didn’t show the “Capability” and “Module” columns and there is no such command as Get-NetIPAddress.

    Then I noticed my console starts with a copyright notice from 2009, whereas the screen shots show 2011. The comments above make it clear that this was done on Win 8 which I believe will ship with PS v3.0, not the current Win 7 (which only shipped with PS v2.0), so this article is pretty useless to me until Win 8 ships or MS releases PS 3.0 for other platforms.

    Thanks for the advance tips – I look forward to playing with PS 3.0 when it’s finalised for production use. How about another article on the getting started with the current version?

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!