Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

See How Naming Makes PowerShell Easy to Learn

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, shares an excerpt from his book, Windows PowerShell 3.0 Step by Step, which explains how the Windows PowerShell naming convention makes it easy to learn.

Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my book, Windows PowerShell 3.0 Step by Step, published by Microsoft Press.

Image of book cover

One of the great things about Windows PowerShell is the verb–noun naming convention. In Windows PowerShell, the verbs indicate an action to perform, such as Set to make a change or Get to retrieve a value. The noun indicates the item with which to work, such as a process or a service.

By mastering the verb–noun naming convention, you can quickly hypothesize what a prospective command might be called. For example, if you need to obtain information about a process, and you know that Windows PowerShell uses the verb Get toretrieve information, you can surmise that the command might very well be Get-Process. To obtain information about services, you try Get-Service…and once again, you are correct.

Note  When you are “guessing” Windows PowerShell cmdlet names, always try the singular form first. Windows PowerShell prefers the singular form of nouns. It is not a design requirement, but it is a strong preference. Therefore, the cmdlets are named Get-Service, Get-Process, and not Get-Services or Get-Processes.

To see the list of approved verbs, use the Get-Verb cmdlet. There are 98 approved verbs in Windows PowerShell 3.0. This number increases the 96 approved verbs from Windows PowerShell 2.0 by only two new verbs. The new verbs are Useand Unprotect. This is shown in the command that follows, where the Measure-Object cmdlet returns the count of the verbs:

PS C:\> (Get-Verb | Measure-Object).count
98

Analyzing Windows PowerShell verb grouping

With nearly 100 verbs, you may be asking yourself, “How does an array of 100 verbs assist in learning Windows PowerShell?” You would be correct in asking that question. Nearly 100 unrelated items are difficult to learn. However, the Windows PowerShell team grouped the verbs. For example, analyzing the common verbs reveals a pattern. The common verbs are listed here:

PS C:\> Get-Verb | where group -match 'common' | Format-Wide verb -auto
Add       Clear     Close     Copy      Enter    Exit     Find     Format   Get
Hide      Join      Lock      Move      New      Open     Optimize Pop      Push
Redo      Remove    Rename    Reset     Resize   Search   Select   Set      Show
Skip      Split     Step      Switch    Undo     Unlock   Watch

The pattern to the verbs emerges when analyzing the verbs: Add/Remove, Enter/Exit, Get/Set, Select/Skip, Lock/Unlock, Push/Pop, and so on. By learning the pattern to the common verbs, you quickly gain a handle on the Windows PowerShell naming convention.

By using the Windows PowerShell verb grouping, you can determine where to focus your efforts. The following command lists the Windows PowerShell verb grouping:

PS C:\> Get-Verb | select group -Unique
Group
-----
Common
Data
Lifecycle
Diagnostic
Communications
Security
Other

Analyzing Windows PowerShell verb distribution

Another way to get a handle on the Windows PowerShell cmdlets, is to analyze the verb distribution. Although there are nearly 100 approved verbs (not to mention rogue developers, who use unapproved verbs), only a fraction of them are utilized repeatedly—and some of them are not used at all in a standard Windows PowerShell installation. By using the Group-Object (group is an alias) and the Sort-Object cmdlet (sort is an alias), the distribution of the cmdlets quickly becomes evident. The following command shows the verb distribution:

Get-Command -CommandType cmdlet | group verb | sort count –Descending

The command and the output associated with the command are shown in the following image:

Image of command output

The preceding output makes it clear that most cmdlets only use a few of the verbs. In fact, most of the cmdlets use only one of ten verbs. This is shown here:

PS C:\> Get-Command -CommandType cmdlet | group verb | sort count -Descending | select -First 10
Count Name                      Group
----- ----                      -----
   94 Get                       {Get-Acl, Get-Alias, Get-AppLockerFileInformation...
   48 Set                       {Set-Acl, Set-Alias, Set-AppLockerPolicy, Set-Aut...
   38 New                       {New-Alias, New-AppLockerPolicy, New-CertificateN...
   30 Remove                    {Remove-AppxProvisionedPackage, Remove-BitsTransf...
   15 Add                       {Add-AppxProvisionedPackage, Add-BitsFile, Add-Ce...
   11 Invoke                    {Invoke-BpaModel, Invoke-CimMethod, Invoke-Comman...
   11 Import                    {Import-Alias, Import-Certificate, Import-Clixml,...
   11 Export                    {Export-Alias, Export-Certificate, Export-Clixml,...
   10 Test                      {Test-AppLockerPolicy, Test-Certificate, Test-Com...
   10 Enable                    {Enable-ComputerRestore, Enable-JobTrigger, Enabl...

In fact, of 436 cmdlets, 278 of the cmdlets use only one of ten verbs. This is shown here:

PS C:\> (Get-Command -CommandType cmdlet | measure).count
436
PS C:\> $count = 0 ; Get-Command -CommandType cmdlet | group verb | sort count -Desce
nding | select -First 10 | % { $count += $_.count ; $count }
94
142
180
210
225
236
247
258
268
278

Therefore, all you need to do is to master 10 verbs, and you will have a good handle on more than half of the cmdlets that ship with Windows PowerShell 3.0.

That is all there is to using Windows PowerShell to explore cmdlet naming. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

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


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>