Find PowerShell Commands by Using the Get-Command Cmdlet

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use the Get-Command cmdlet in Windows PowerShell to find various commands.

Hey, Scripting Guy! Question Hey, Scripting Guy! It all seems so easy for you. You want to find something by using Windows PowerShell and ba-da-bing, it is there. But for those of us who have not been using Windows PowerShell since before it was ever released, what is the secret? I mean using Windows PowerShell does not seem hard—but there is so much of it. Every day, it seems that you perform this act, and it is like Windows PowerShell theatre. Of course, it works, but how did you find it in the first place? I hope I am making myself clear…I do not even know where to start.

—RS

Hey, Scripting Guy! Answer Hello RS,

Microsoft Scripting Guy, Ed Wilson, is here. Today the day seems late, or early, depending on one’s perspective. It is nearly noon, and the sun does not appear to have awakened. The skies are all gray, and a thin drizzle of rainfall is providing a nice drink of cool water for our parched trees and plants. The Scripting Wife and I took our tea on the lanai this morning as we discussed Microsoft TechEd 2012 in Orlando.

“You need to get everything planned,” she said. “TechEd will be here sooner than you think.”

“Actually, I think TechEd will arrive exactly on Monday, June 11. We have to have the Scripting Guys booth set up by noon on June 10. That is also when we will see Daniel Cruz, who is helping out in the booth. We may also get to see Rohn Edwards and Lido Paglia, the 2012 Scripting Games winners,” I said.

“It is going to be so much fun,” she replied, “And the TechEd party is going to be held at Universal Islands of Adventure theme park. How cool is that?”

“That is true, but I am really just looking forward to meeting people and to making new friends. For me, TechEd is all about the networking opportunities.”

“So, speaking of networking,” she began, “How are you doing about creating the Scripting Guys guest schedule?”

“Well, so far, I have Mark Minasi, Don Jones, Jeffery Hicks, Jeffrey Snover, and a few others scheduled to make guest appearances at the Scripting Guys booth. I also have a confirmation from O’Reilly press for my autograph session at their booth.”

“So are you going to let me in on the secret? Or are you going to keep it all to yourself?” she asked.

“Well, for now, I will keep it to myself,” I said with a smile.

“Well, then for now, I am heading out with my friends,” she said with a smile, “I think we are going to check out that new store that opened up near the interstate.”

And with no further ado, she was gone.

So, RS, I decided to head upstairs to check out the email sent to scripter@microsoft.com, and I ran across your email.

Note   Tomorrow night, May 16, 2012 at 9:30 PM Eastern Standard Time (-5 GMT), the two winners of the 2012 Scripting Games (Lido Paglia and Rohn Edwards), Jeffrey Snover, and myself all appear with Jonathan Walz and Hal Rottenberg on the PowerScripting Podcast. This event is recorded live, and there is a chat room set up so you can talk to your fellow listeners, in addition to asking questions of the guests. It will be a lot of fun, and you should not miss it if at all possible.

The easy way to find Windows PowerShell cmdlets

The first thing to keep in mind is that in Windows PowerShell, not everything is a cmdlet. There are language statements, functions, aliases, various objects (from the .NET Framework or COM), and even other executables—all of which are utilizable from within Windows PowerShell. In Windows 8 Consumer Preview, this means you have around 1000 commands from which to choose. In Windows 7, the situation is not quite so overwhelming, but still you need to know how to find what you want.

In Windows 7, much of the power of Windows PowerShell comes from WMI. (This is actually true in Windows 8 Consumer Preview also, but the WMI classes are exposed more directly.) For information about searching and working with WMI, see this collection of Hey, Scripting Guy! Blogs.

Note    In yesterday’s blog, Discover the Easy Way to Begin Learning Windows PowerShell, I talked about learning the Windows PowerShell Verb-Noun naming pattern as a way to develop an understanding of Windows PowerShell coverage. This technique will also aid in finding Windows PowerShell cmdlets.

Two cmdlets are essential for discovering Windows PowerShell commands. The first is the Get-Command cmdlet, and the second is the Get-Help cmdlet. At first glance, the Get-Command cmdlet might not appear to be all that useful. For example, you provide it with the name of a cmdlet, and basically what returns is the name of the cmdlet. This command is shown here.

Get-Command Get-Process

The command and the output associated with the command illustrate the problem. The default output does not appear to display much more information than you knew when you typed in the command. You figured it was a cmdlet, you knew the name of Get-Process, and the definition does not add much additional information.

Image of command output

But, remember, everything in Windows PowerShell is an object. In fact, the Get-Command cmdlet returns a CmdletInfo object. This is shown here.

PS C:\> Get-Command Get-Process | gm

 

   TypeName: System.Management.Automation.CmdletInfo

 

Name                MemberType     Definition

—-                ———-     ———-

Equals              Method         bool Equals(System.Object obj)

GetHashCode         Method         int GetHashCode()

GetType             Method         type GetType()

ToString            Method         string ToString()

CommandType         Property       System.Management.Automation.CommandTypes Comm…

DefaultParameterSet Property       System.String DefaultParameterSet {get;}

Definition          Property       System.String Definition {get;}

HelpFile            Property       System.String HelpFile {get;}

ImplementingType    Property       System.Type ImplementingType {get;}

Module              Property       System.Management.Automation.PSModuleInfo Modu…

ModuleName          Property       System.String ModuleName {get;}

Name                Property       System.String Name {get;}

Noun                Property       System.String Noun {get;}

OutputType          Property       System.Collections.ObjectModel.ReadOnlyCollect…

Parameters          Property       System.Collections.Generic.Dictionary`2[[Syste…

ParameterSets       Property       System.Collections.ObjectModel.ReadOnlyCollect…

PSSnapIn            Property       System.Management.Automation.PSSnapInInfo PSSn…

Verb                Property       System.String Verb {get;}

Visibility          Property       System.Management.Automation.SessionStateEntry…

DLL                 ScriptProperty System.Object DLL {get=$this.ImplementingType….

HelpUri             ScriptProperty System.Object HelpUri {get=try…

Based on the members of the CmdletInfo object, there appears to be a lot of information available about the cmdlet. The easiest way to view this information is to pipe the output from the Get-Command cmdlet to the Format-List cmdlet and to use a wild card character to choose all available properties. The command is shown here (fl is an alias for the Format-List cmdlet).

Get-Command Get-Process | fl *

The command, and the output associated with the command are shown in the image that follows.

Image of command output

In yesterday’s Hey, Scripting Guy! Blog, I talked about working with Windows PowerShell verbs as a way of understanding available commands. When you know and understand the various verbs, using the Get-Command cmdlet becomes much more valuable. For example, when you are looking for information about various items, you know you will more than likely use the get verb. Therefore, use the Get-Command cmdlet to retrieve only cmdlets that use the get verb. This command is shown here.

Get-Command –verb get

If you seek cmdlets that will assign a new value to something, you more than likely are looking for a cmdlet that uses the set verb. The following command retrieves these types of cmdlets.

Get-Command –verb set

Use nouns for cmdlets

One of the things that tends to confuse beginners is the difference between the name and the noun parameters. The Get-Command cmdlet has multiple parameter sets, and when you use the verb or the noun parameter, the Get-Command cmdlet only returns cmdlets. If you use the name parameter, Get-Command finds cmdlets, executables, functions, aliases, and other types of commands.

A good way to find commands is to use wild cards. For example, the following command returns any command containing the letters adapter within the name of the command. The output reveals that one command meets this filter.

PS C:\> gcm -Name *adapter*

CommandType     Name                               Definition

———–     —-                               ———-

Application     AdapterTroubleshooter.exe          C:\Windows\system32\AdapterTro…

Note   Because the Get-Command cmdlet returns more than Windows PowerShell cmdlets, I often use it to help me locate various Windows executables. Because the Windows PowerShell console is generally open on my computer, it is faster for me to use than to use Windows Search.

In the following image, I first look for commands related to process. The first results (obtained by using the name parameter) contain a number of applications, in addition to several Windows PowerShell cmdlets. When I limit the results to only cmdlets that have a noun related to process the results are more directed.

Image of command output

RS, that is all there is to using the Get-Command cmdlet to search for cmdlets. Join me tomorrow when I will talk about additional ways to use Get-Command. It will be a very cool blog (and one not only for beginners).

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon