Summary: Ed Wilson, Microsoft Scripting Guy, talks about the five best Windows PowerShell cmdlets.
Microsoft Scripting Guy, Ed Wilson, is here. OK, I will admit that this post is completely arbitrary. What do I mean when I talk about the five best Windows PowerShell cmdlets (commands)? Well, I am thinking about the cmdlets I use on a daily basis and the cmdlets that make my life really easy.
The best cmdlet: Get-Command
For me, the best Windows PowerShell cmdlet is probably Get-Command. I use this cmdlet every single day. In fact, I often use it multiple times a day. Why? Because I simply cannot remember thousands of cmdlet names. Even if I do remember thousands of cmdlet names, I cannot remember their various command sets and all of the available parameters. Here is an example:
S C:\> Get-Command -Noun *tcp*
CommandType Name ModuleName
----------- ---- ----------
Function Get-NetTCPConnection NetTCPIP
Function Get-NetTCPSetting NetTCPIP
Function Send-TcpRequest PowerShellCookbook
Function Set-NetTCPSetting NetTCPIP
Other cool Windows PowerShell cmdlets
Get-Help
When I want to see exactly what a new cmdlet will do or find detailed information about a particular parameter, I turn to Get-Help. I also like to use Get-Help to look for examples of how to use a new cmdlet. Here is an example:
PS C:\> get-help Get-Command -Parameter verb
-Verb <String[]>
Gets commands (cmdlets, functions, workflows, and aliases) that have names that include the
specified verb. Enter one or more verbs or verb patterns. Wildcards are permitted.
Required? false
Position? named
Default value All verbs
Accept pipeline input? True (ByPropertyName)
Accept wildcard characters? false
Get-Member
Everyone knows that Windows PowerShell is object-oriented and that it returns objects instead of strings. But what kind of object is actually returned? What are the members, properties, and methods that are available from a specific object? To answer these and other types of questions such as this, I use Get-Member. Here is an example:
PS C:\> Get-Command | Get-Member -MemberType ScriptProperty
TypeName: System.Management.Automation.AliasInfo
Name MemberType Definition
---- ---------- ----------
DisplayName ScriptProperty System.Object DisplayName {get=if ($this.Name.IndexOf('-') -lt 0)...
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPref...
ResolvedCommandName ScriptProperty System.Object ResolvedCommandName {get=$this.ResolvedCommand.Name;}
TypeName: System.Management.Automation.FunctionInfo
Name MemberType Definition
---- ---------- ----------
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference...
TypeName: System.Management.Automation.FilterInfo
Name MemberType Definition
---- ---------- ----------
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference...
TypeName: System.Management.Automation.CmdletInfo
Name MemberType Definition
---- ---------- ----------
DLL ScriptProperty System.Object DLL {get=$this.ImplementingType.Assembly.Location;}
HelpUri ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference...
The big three
Anyone who has ever read a book about Windows PowerShell, attended a class about Windows PowerShell, or even attended a one-hour introduction to Windows PowerShell session at a community event has heard about the big three cmdlets:
- Get-Command
- Get-Help
- Get-Member
The cool thing about Windows PowerShell is that it is self-discovering. This means that I can use Windows PowerShell to discover how to use Windows PowerShell and how to continue to learn more things about Windows PowerShell.
I continue to use these three cmdlets every day, so learning nuances about the cmdlets pays great dividends. And how do I learn nuances about these three cmdlets? Well, by using the three cmdlets. For example, I can use the following commands:
- Get-Help Get-Help
- Get-Command Get-Help
- Get-Help | Get-Member
The other two
The other two cmdlets that I use on a daily basis are Select-Object and Out-GridView. Both of these cmdlets are very useful, and they help me solve problems. In addition, Out-GridView is a great exploring cmdlet.
Select-Object
One reason I use Select-Object so much is that it is an easy way to create a custom object. I can even use a script block to calculate a value, and then use the result as a value of a custom property. Because Windows PowerShell is object-oriented, the Select-Object cmdlet becomes even more important. Here is an example:
PS C:\> Get-Help Get-Member | Select-Object name, synopsis
Name Synopsis
---- --------
Get-Member Gets the properties and methods of objects.
Out-GridView
I use Out-GridView so much because it is an easy intermediate step that permits me to select columns or properties, filter data, and visualize what I am actually dealing with. Here is an example of such a command:
Get-Command -Verb get | Get-Help | Select-Object name, synopsis | Out-GridView
The output is shown in the following image:
That is all there is to using the five best Windows PowerShell cmdlets. Join me tomorrow when I will talk about more Windows PowerShell coolness.
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