#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Get-PWCommands FUNCTION

I want to share a function I created to quickly find a cmdlet(s) within the PWPS_DAB module.  I realize you can use the Get-Command cmdlet or the Commands window with PowerShell to do the same.  However, for me, I am constantly trying to remember the name of a cmdlet(s) and using one of the other tools just takes to long. ( Again, for me. )

So, this function will offer a way to quickly get a list of cmdlets from the PWPS_DAB module by Verb, Noun, Name or a combination of Verb and Noun. At the time of this post, the PWPS_DAB module contained 498 cmdlets.

Function

The following is the function definition and the help information.

FUNCTION Get-PWCommands {
    <#
        .SYNOPSIS
        Used to get ONLY commands contained in the PWPS_DAB module.
        .DESCRIPTION
        Returns ONLY commands related to the PWPS_DAB module. 
        .EXAMPLE
        # Returns all commands delivered with the PWPS_DAB module.
        Get-PWCommands -Verbose
        # Returns all commands with the word userlist in the cmdlet name.
        Get-PWCommands -Name 'userlist'
        # Returns all commands with the word Get in the verb portion of the cmdlet name.
        Get-PWCommands -Verb 'get'
        # Returns all commands with the word group in the noun portion of the cmdlet name.
        Get-PWCommands -Noun 'group'
        # Returns all commands with the word update in the verb portion of the cmdlet name and user in the noun portion of the cmdlet name.
        Get-PWCommands -Verb update -Noun user
#>
...
}

Parameters

There are three parameters available, Name, Noun, and Verb. None of then are required and one or more can be included.

[CmdletBinding()]
param (
    [ValidateNotNullOrEmpty()]
    [Parameter(
        Position = 0,
        HelpMessage = "Specifies the name of the PowerShell cmdlet to search for."
    )]
    [string] $Name,

    [ValidateNotNullOrEmpty()]
    [Parameter(
        HelpMessage = "Specifies the 'noun' within the PowerShell cmdlet name to search for."
    )]
    [string] $Noun,

    [ValidateNotNullOrEmpty()]
    [Parameter(
        HelpMessage = "Specifies the 'verb' within the PowerShell cmdlet name to search for."
    )]
    [string] $Verb
) # end

Code

The following shows the meat of the function. I have included the BEGIN, PROCESS, and END code blocks even though not all are used. This is to maintain my own formatting. Obviously, you can exclude those if you like.

You can see that I am taking advantage of the Get-Command cmdlet, however, I am limiting the scope to the PWPS_DAB module. Also, all parameter values are being wildcarded by default to eliminate the need to enter the wildcards within the provided parameter values. Simplifies the process.

BEGIN { <# Nothing to do here #>} # end BEGIN...

PROCESS {

        $Splat_GetCommand = @{
            Module = 'PWPS_DAB'
        }
        if( -not ([string]::IsNullOrEmpty($Name))) {
            $Splat_GetCommand.Name = "*$Name*"
        }
        if( -not ([string]::IsNullOrEmpty($Noun))) {
            $Splat_GetCommand.Noun = "*$Noun*"
        }
        if( -not ([string]::IsNullOrEmpty($Verb))) {
            $Splat_GetCommand.Verb = "*$Verb*"
        }

        $ReturnValue = Get-Command @Splat_GetCommand | Select-Object Name
    } # end PROCESS...

    END {
        Write-Output $ReturnValue
    } # end END...
} # end FUNCTION Get-PWCommands...

Getting Commands

Now that you have the function, lets see what you can do with it.

Get ALL Commands

To return all commands, you simply call the function without including any parameters.

# Get all cmdlets in the PWPS_DAB Module.
Get-PWCommands

The following shows a small portion of the results. Again, there are nearly 500 cmdlets, so listing them all would be foolish.

allcmdlets

Get-Cmdlets By Name

When using the Name parameter, all cmdlets containing the provided value will be returned. This is regardless of whether the value is in the verb or noun.  Also, this cmdlet is set to use the the positional value of zero.  So, if you don’t specify a parameter and you pass a value, it will be assumed to use Name.

 # Get cmdlets with the provided name in the PWPS_DAB Module.
Get-PWCommands -Name set

The following shows a portion of the results.

Notice that the word set is included somewhere in all of the cmdlets. This includes both the verb and noun parts of the cmdlet names.

name

Get Cmdlets by Verb

Here we will get all cmdlets which use the verb ‘Update’.

# Get cmdlets with the provided verb in the PWPS_DAB Module.
Get-PWCommands -Verb update

The following shows the results.

Notice all cmdlets contain the verb Update.

verb

Get Cmdlets by Noun

Here we will get all cmdlets which use the noun environment.

# Get cmdlets with the provided noun in the PWPS_DAB Module.
Get-PWCommands -Noun environment

The following shows the results.

Notice that there are different verbs being returned and all nouns contain the word environment.

noun

Get Cmdlets by Verb and Noun

Finally, we will provide a verb and a noun combination to filter the results a bit more.

# Get cmdlets with the provided verb and noun combination in the PWPS_DAB Module.
Get-PWCommands -Verb get  -Noun document

The following shows all of the cmdlets containing the verb get and the noun document.

Notice, every cmdlet has the verb Get and every noun contains the word Document.

getdocuments

Summary

Again, the reason for this function is to expedite the process of getting cmdlet names available in the PWPS_DAB module. By providing one or more of the parameters, the returned list of cmdlet names will be filtered down. For me it is much quicker than using the Command window or the Get-Command cmdlet.


Experiment with it and have fun. And please, let me know if there is a topic you would like to see a post for. Or share a solution you have developed.

The following is a link to the complete psm1 file.

FUNCTION_Get-PWCommands

Hopefully, you find this useful. Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page. And thank you for checking out my blog.

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.