Earlier this week I exchanged a few tweets with @jonhtyler about coming up with a proper name for a PowerShell function he was developing. The suggested best practice is to use the Verb-Noun naming convention, using an accepted verb. You can see the verbs with the Get-Verb cmdlet. So I figured why not automate this a bit. After all, sometimes it can be tricky finding the proper verb. Sometimes it helps to actually see the name first. So I put together this simple function called Test-CommandName.
#comment help is here
Param(
[Parameter(Position=0,Mandatory=$True,
HelpMessage="What is the noun for your command?")]
[ValidateNotNullorEmpty()]
[string]$Noun,
[ValidateSet("All","Common","Data","Lifecycle","Diagnostic","Communications","Security","Other")]
[string]$Category="All"
)
if ($Category -eq "All") {
#get all verb names and add to an array
$verbs = Get-Verb | Select -ExpandProperty Verb
}
else {
#get verbs that belong to the given category and add to an array of verb names
$verbs = Get-Verb | Where {$_.Group -eq $Category} | Select -ExpandProperty Verb
}
foreach ($verb in $verbs) {
<#
go through each verb and display a string that
represents a potential Verb-Noun name for
your command
#>
"{0}-{1}" -f $verb,$noun
}
} #end Test-CommandName function
The function takes the noun you want to use as a parameter. By default, the function will combine it with all possible verbs and write a string with each file name to the pipeline. But you can specify a verb category if you want. Notice the use of the ValidateSet decorator. The rest of the code is commented and should be pretty easy to follow. But now I can do something like this:
Backup-DataFile
Checkpoint-DataFile
Compare-DataFile
Compress-DataFile
Convert-DataFile
ConvertFrom-DataFile
ConvertTo-DataFile
Dismount-DataFile
Edit-DataFile
Expand-DataFile
Export-DataFile
Group-DataFile
Import-DataFile
Initialize-DataFile
Limit-DataFile
Merge-DataFile
Mount-DataFile
Out-DataFile
Publish-DataFile
Restore-DataFile
Save-DataFile
Sync-DataFile
Unpublish-DataFile
Update-DataFile
Just a little PowerShell Friday Fun, but maybe you’ll pick up a tip or two. Download Test-CommandName and let me know what you think. The download version includes comment based help and should work in PowerShell v2 or v3.