Skip to main content

Find all PowerShell Commands with Specific Parameters

My PowerShell class this week had a great idea.  While we were utilizing Get-Command to find commands, someone asked if it was possible to find commands based on their parameters.  I thought that that would be a good idea.  The original code started really simple.

1

2

3

4

5

6

7

8

Function Find-Parameters

{

    Param(

        [String]$Parameter

    )

    Get-Command |

    Where-Object {$_.Parameters.Keys -eq $Parameter}

}

This code was designed to be placed in their profiles for easy use.  They simple needed to type Find-Parameters and then the parameter name that they were interested in.  Out popped all the commands that contained that parameter.

I decided to have a bit more fun with it.  First I turned it into a filtering function that allowed you to search for commands with multiple parameters through the pipeline.  The –ExactMatch switch was added to filter out all but the commands that contained all the parameters in the search.

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

 

<#

.SYNOPSIS

   Finds all cmdlets with the specified parameters.

.DESCRIPTION

   Allows you to search all of the cmdlets on a client for cmdlets that

   contain those parameters

.PARAMETER Parameter

   A list of parameters you want in the returned commands.

.PARAMETER ExactMatch

   Only returns commands that contain all of the provided

   parameters

.EXAMPLE

   "ComputerName","PassThru" | Find-Parameters -Verbose -ExactMatch

 

   Command

   -------                          

   Add-ADComputer

   Remove-Computer

   Rename-Computer

   Set-Service 

 

   Returns only commands that have "ComputerName" and "PassThru" as

   parameters.

#>

Function Find-Parameters

{

    [CmdletBinding()]

    Param(

        [parameter(Mandatory=$true,

                      ValueFromPipeline=$true)]

        [String[]]$Parameter,

 

        [Switch]$ExactMatch

    )

    BEGIN

    {

        $Result = $null

        $FinalResult = $null

    } # END: BEGIN BLOCK

   

    PROCESS

    {

       

        # Loop through each command and determine if

        # it contains one of the parameters.

        ForEach ($P in $Parameter)

        {

            $Result = Get-Command |

            Where-Object {$_.Parameters.Keys -eq $P}

        }

       

        If ($ExactMatch -and ($FinalResult -ne $null))

        {

            # If "ExactMatch" is used and "$FinalResult" is not $null,

            # then Compare what is in $Result with $Final result and

            # save all instances where there are identical data into

            # $FinalResult.

            $CompareHash = @{

                'ReferenceObject' = $Result

                'DifferenceObject' = $FinalResult

                'ExcludeDifferent' = $True

                'IncludeEqual' = $True

            }

            $FinalResult = Compare-Object @CompareHash |

                Select-Object -Property @{'n'= "Command"; 'e'={$_.InputObject}}

        }

          ElseIf (($FinalResult -ne $null) -and ($ExactMatch -eq $False))

        {

            # If -ExactMatch is not $true then add the new

            # results to $FinalResult, and then filter

            # so all commands are unique to prevent repeated

            # data.

            $FinalResult = $FinalResult += $Result |

                Select-Object -Unique

        }

        Else

        {

            # If this is the first parameter queried or only one

            # is provided, save the results in $FinalResult

            $FinalResult = $Result

        }

 

    } # END: PROCESS BLOCK

 

    END

    {

        # Sort the data and send it to the pipeline.

        Write-Output ($FinalResult |        

            Sort-Object -Property Name)

    } # END: END BLOCK

 

} 

   

Lines 1-24 is the help file.

Lines 35-39 is the BEGIN block.  It  initializes our two variables.  $Result is the temporary storage and $FinalResult is what will be sent to the pipeline.

Lines 46 – 50 look very similar to the original code.  This time multiple parameters can be searched for.  The IF Statement from lines 52-81 is where the magic happens.

Let’s start in the ELSE block.  It simply writes the results to $FinalResult.  This is ran only if 1 parameter is given or the –ExactMatch switch is not used.

The ELSEIF block is used if multiple parameters are being search for, but the user does not care if all of the parameters are present in each command.

The IF block takes each result, and compares it to the first.  It allows only commands that are present in both results to continue on.  This allows –ExactMatch to work.

Lines 85 – 90 Sorts the data and then sends it to the pipeline.

I like this one so much, I’ve added it to my module of helper functions.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.