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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...