Find commands with parameter names

This topic contains 13 replies, has 6 voices, and was last updated by  Curtis Smith 3 weeks, 1 day ago.

  • Author
    Posts
  • #101089

    Harsha
    Participant

    Hello,
    Trying to find the list of cmdlets which has Port parameter defined. I am expecting Test-NetConnection cmdlet in the result pane after executing

    Get-Command -ParameterName *Port* -All

    But, I see

      Microsoft.PowerShell.Core
      Microsoft.PowerShell.Management

    module cmdlets.

    Am I doing anything wrong here? Appreciate your help!

    Thanks

  • #101091

    Alex Aymonier
    Participant

    Hi Harsha,

    What version are you using? I've tried in V5.1 and 6 and get the following.

    Without -all

    PS H:\> Get-Command -ParameterName *Port*
    
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Function        New-PSWorkflowSession                              2.0.0.0    PSWorkflow
    Cmdlet          Connect-PSSession                                  3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Enter-PSSession                                    3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Get-Command                                        3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Get-PSSession                                      3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Import-LocalizedData                               3.1.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Invoke-Command                                     3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-ModuleManifest                                 3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-PSRoleCapabilityFile                           3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-PSSession                                      3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-PSSessionConfigurationFile                     3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-PSSessionOption                                3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Receive-PSSession                                  3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Register-EngineEvent                               3.1.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Register-ObjectEvent                               3.1.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Register-PSSessionConfiguration                    3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Register-WmiEvent                                  3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          Send-MailMessage                                   3.1.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Set-PSSessionConfiguration                         3.0.0.0    Microsoft.PowerShell.Core
    

    And with -all I get

    PS H:\> Get-Command -ParameterName *Port* -all
    
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Alias           cnsn -> Connect-PSSession
    Alias           etsn -> Enter-PSSession
    Alias           gcm -> Get-Command
    Alias           gsn -> Get-PSSession
    Alias           icm -> Invoke-Command
    Alias           npssc -> New-PSSessionConfigurationFile
    Alias           nsn -> New-PSSession
    Alias           nwsn -> New-PSWorkflowSession                      2.0.0.0    PSWorkflow
    Alias           rcsn -> Receive-PSSession
    Function        New-PSWorkflowSession                              2.0.0.0    PSWorkflow
    Cmdlet          Connect-PSSession                                  3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Enter-PSSession                                    3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Get-Command                                        3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Get-PSSession                                      3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Import-LocalizedData                               3.1.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Invoke-Command                                     3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-ModuleManifest                                 3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-PSRoleCapabilityFile                           3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-PSSession                                      3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-PSSessionConfigurationFile                     3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          New-PSSessionOption                                3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Receive-PSSession                                  3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Register-EngineEvent                               3.1.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Register-ObjectEvent                               3.1.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Register-PSSessionConfiguration                    3.0.0.0    Microsoft.PowerShell.Core
    Cmdlet          Register-WmiEvent                                  3.1.0.0    Microsoft.PowerShell.Management
    Cmdlet          Send-MailMessage                                   3.1.0.0    Microsoft.PowerShell.Utility
    Cmdlet          Set-PSSessionConfiguration                         3.0.0.0    Microsoft.PowerShell.Core
    
    • #101101

      Harsha
      Participant

      Hello Alex,
      It is 5.1 Version on Windows 2012 R2. My results are similar to yours. None of them retrieved the Test-NetConnection cmdlet even though I have NetTCPIP module installed. It is showing only after importing the module into current session.

      Thanks

  • #101095

    postanote
    Participant

    Here is a little snippet I pulled together and keep around for such search and find efforts.
    Maybe more than you need at this point, but should show you what you can do.

    # Get parameters, examples, full and Online help for a cmdlet or function
    
    # Get a list of all functions
    Get-Command -CommandType Function | 
    Out-GridView -PassThru -Title 'Available functions'
    
    # Get a list of all commandlets
    Get-Command -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available cmdlets'
    
    # Get a list of all functions for the specified name
    Get-Command -Name '*ADGroup*' -CommandType Function | 
    Out-GridView -PassThru -Title 'Available named functions'
    
    # Get a list of all commandlets for the specified name
    Get-Command -Name '*ADGroup**'  -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available named cmdlet'
    
    # get function / cmdlet details
    (Get-Command -Name Get-ADUser).Parameters
    Get-help -Name Get-ADUser -Examples
    Get-help -Name Get-ADUser -Full
    Get-help -Name Get-ADUser -Online
    
    Get-Help about_*
    Get-Help about_Functions
    
    # Find all cmdlets / functions with a target parameter
    Get-Command -CommandType Function | 
    Where-Object { $_.parameters.keys -match 'credential'} | 
    Out-GridView -PassThru -Title 'Available functions which has a specific parameter'
    
    Get-Command -CommandType Cmdlet | 
    Where-Object { $_.parameters.keys -match 'credential'} | 
    Out-GridView -PassThru -Title 'Results for cmdlets which has a specific parameter'
    
    # Get named aliases 
    Get-Alias | 
    Out-GridView -PassThru -Title 'Available aliases'
    
    # Get cmdlet / function parameter aliases
    (Get-Command Get-ADUser).Parameters.Values | 
    where aliases | 
    select Name, Aliases | Out-GridView -PassThru -Title 'Alias results for a given cmdlet or function.'
    
    
    # All Help topics locations
    explorer "$pshome\$($Host.CurrentCulture.Name)"
    
    # Get any .NET types and their static methods from PowerShell. 
    # Enumerate all that are currently loaded into your AppDomain.
    #  
    [AppDomain]::CurrentDomain.GetAssemblies() | 
    foreach { $_.GetTypes() } | 
    foreach { $_.GetMethods() } | 
    where { $_.IsStatic } | 
    select DeclaringType, Name | 
    Out-GridView -PassThru -Title '.NET types and their static methods'
    
    # Instantiate the types using new-object and call instance methods. 
    # You can use get-member on an instance to get the methods on a type.
    
    • #101151

      Harsha
      Participant

      Thanks for sharing this snippet.It is so useful for new-comers like me.

  • #101113

    Jon
    Participant

    Working as designed, per the documentation

    -ParameterName
    Specifies an array of parameter names. This cmdlet gets commands in the session that have the specified parameters. Enter parameter names or parameter aliases. Wildcard characters are supported.

  • #101154

    Harsha
    Participant

    Jon,
    Thanks for pointing that. So, I believe the discoverability of commands with required parameters looks quite hard then – IMO (For starters atleast). Ideally, I should import all modules (most often used) into the session and do the search- I guess

  • #101202

    js
    Participant

    Hmm, get-command returns a .parameters property, but it's a dictionary.

    Hmm, work in progress. Just need the original command in there.

    $a = get-command
    $b = $a.parameters
    $b.Keys | where { $_ -match 'port' }
  • #101248

    Curtis Smith
    Participant
    Get-Module -ListAvailable | Import-Module
    get-command -all | Where-Object {$_.Parameters.Keys -contains 'port'}
    
    • #101251

      js
      Participant

      Hah, you beat me. Or

      get-module -ListAvailable | import-module
      get-command -ParameterName *port*
  • #101259

    js
    Participant

    A weird thing is doing something like "get-command test-connection" will load the module that it's in (Microsoft.PowerShell.Management). (This is 6.1PR2 on my mac.)

    $a = get-command test-connection                                                                                                  
    get-command -ListImported | group source | ft -AutoSize                                                                           
    
    Count Name                            Group
    ----- ----                            -----
       20                                 {cd.., cd\, Clear-Host, com...}
        6 PSReadLine                      {PSConsoleHostReadline, Get-PSReadlineKeyHandler, Get-PSReadlineOption, Remove-PSReadlineKeyHandler...}
       43 Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty...}
       38 Microsoft.PowerShell.Core       {Add-History, Clear-History, Debug-Job, Enter-PSSession...}
      103 Microsoft.PowerShell.Utility    {Add-Member, Add-Type, Clear-Variable, Compare-Object...}

    And that leads to another way. Just "get-command | get-command" won't work.

    get-command | select name | get-command -ParameterName *port*

    Ah, putting /pre on a new line creates a blank line in a code block.

    More mischief with passing multiple parameters over the pipe when one parameter can be passed byvalue. Powershell attempts the useless converting of a hash-table into a string for the name parameter.

    [pscustomobject]@{totalcount=1} | get-command                                                                            
    
    get-command : The term '@{totalcount=1}' is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:35
    + [pscustomobject]@{totalcount=1} | get-command
    +                                   ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{totalcount=1}:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
  • #101305

    Curtis Smith
    Participant

    So Get-command by itself sees all the command/functions available, but does not load the necessary modules if they are currently unloaded to see the parameters.

    IE.

    PS C:\> get-command | Where-Object {$_.name -eq 'Test-NetConnection'} | Select *
    
    
    HelpUri             :
    ScriptBlock         :
    CmdletBinding       : False
    DefaultParameterSet :
    Definition          :
    Options             : None
    Description         :
    Verb                : Test
    Noun                : NetConnection
    HelpFile            :
    OutputType          : {}
    Name                : Test-NetConnection
    CommandType         : Function
    Visibility          : Public
    ModuleName          : NetTCPIP
    Module              : NetTCPIP
    RemotingCapability  : PowerShell
    Parameters          : {}
    ParameterSets       : {}

    But passing the command as a parameter causes get-command to load the necessary module to see the parameters

    IE.

    get-command 'Test-NetConnection' | Select *
    
    ...
    Module              : NetTCPIP
    RemotingCapability  : SupportedByCommand
    Parameters          : {[ComputerName, System.Management.Automation.ParameterMetadata], [TraceRoute,
                          System.Management.Automation.ParameterMetadata], [Hops,
                          System.Management.Automation.ParameterMetadata], [CommonTCPPort,
                          System.Management.Automation.ParameterMetadata]...}
    ParameterSets       : {[[-ComputerName] ] [-TraceRoute] [-Hops ] [-InformationLevel ]
                          [], [[-ComputerName] ] [-CommonTCPPort]  [-InformationLevel
                          ] [], [[-ComputerName] ] -Port  [-InformationLevel
                          ] []}
    

    That is why piping get-command into a select-object and then back to get-command could work. You just need a slight modification to your command to expand the name property so that the second get command is just receiving the name value.

    IE.

    PS C:\> get-command | Select-Object -ExpandProperty Name | Get-command -ParameterName "port"
    
    CommandType     Name                                               ModuleName
    -----------     ----                                               ----------
    Function        Get-PcsvDevice                                     PcsvDevice
    Function        New-NetQosPolicy                                   NetQos
    Function        New-PSWorkflowSession                              PSWorkflow
    Function        Restart-PcsvDevice                                 PcsvDevice
    Function        Set-NetQosPolicy                                   NetQos
    Function        Set-PcsvDeviceBootConfiguration                    PcsvDevice
    Function        Start-PcsvDevice                                   PcsvDevice
    Function        Stop-PcsvDevice                                    PcsvDevice
    Function        Test-NetConnection                                 NetTCPIP
    Cmdlet          Connect-PSSession                                  Microsoft.PowerShell.Core
    Cmdlet          Connect-WSMan                                      Microsoft.WSMan.Management
    Cmdlet          Enter-PSSession                                    Microsoft.PowerShell.Core
    Cmdlet          Get-PSSession                                      Microsoft.PowerShell.Core
    Cmdlet          Get-WSManInstance                                  Microsoft.WSMan.Management
    Cmdlet          Invoke-Command                                     Microsoft.PowerShell.Core
    Cmdlet          Invoke-WSManAction                                 Microsoft.WSMan.Management
    Cmdlet          Join-DtcDiagnosticResourceManager                  MsDtc
    Cmdlet          New-CimSession                                     CimCmdlets
    Cmdlet          New-PSSession                                      Microsoft.PowerShell.Core
    Cmdlet          New-WSManInstance                                  Microsoft.WSMan.Management
    Cmdlet          Receive-DtcDiagnosticTransaction                   MsDtc
    Cmdlet          Receive-PSSession                                  Microsoft.PowerShell.Core
    Cmdlet          Remove-WSManInstance                               Microsoft.WSMan.Management
    Cmdlet          Send-DtcDiagnosticTransaction                      MsDtc
    Cmdlet          Send-MailMessage                                   Microsoft.PowerShell.Utility
    Cmdlet          Set-WSManInstance                                  Microsoft.WSMan.Management
    Cmdlet          Start-DtcDiagnosticResourceManager                 MsDtc
    Cmdlet          Test-WSMan                                         Microsoft.WSMan.Management
  • #101320

    js
    Participant

    This actually works fine:

    get-command | select name | get-command -ParameterName *port*
  • #101361

    Curtis Smith
    Participant

    So it does. Nice. 🙂

You must be logged in to reply to this topic.