Find PowerShell-Cmdlets with no parameters

50 Views Asked by At

I would like to get all PowerShell-Cmdlets in the current session that have no parameters (if they are any).

When I try

Get-Command -CommandType Cmdlet | Where-Object { $_.Parameters.Count -eq 0 } | Select-Object -Property Name, @{n="PCount";e={$_.Parameters.Count}}

I'll get a lot of Cmdlets. But it turns out that they actually have parameters:

(Get-Command -Name Write-Host).Parameters.Count

What is the correct way to get Cmdlets that have no parameters?

1

There are 1 best solutions below

3
Santiago Squarzon On BEST ANSWER

You should get no results from your filter however, as you stated you're getting wrong results and this is most likely due to lazy loading of the parameter info of each command, a consecutive piping to Get-Command forces the cmdlets' module to be loaded then the issue is gone, using CimCmdlets Module as example:

PS ..\pwsh> Get-Command -CommandType Cmdlet |
    Where-Object { $_.Parameters.Count -eq 0 -and $_.ModuleName -eq 'CimCmdlets' }


CommandType     Name                                               Version    Source    
-----------     ----                                               -------    ------    
Cmdlet          Export-BinaryMiLog                                 7.0.0.0    CimCmdlets
Cmdlet          Get-CimAssociatedInstance                          7.0.0.0    CimCmdlets
Cmdlet          Get-CimClass                                       7.0.0.0    CimCmdlets
Cmdlet          Get-CimInstance                                    7.0.0.0    CimCmdlets
Cmdlet          Get-CimSession                                     7.0.0.0    CimCmdlets
Cmdlet          Import-BinaryMiLog                                 7.0.0.0    CimCmdlets
Cmdlet          Invoke-CimMethod                                   7.0.0.0    CimCmdlets
Cmdlet          New-CimInstance                                    7.0.0.0    CimCmdlets
Cmdlet          New-CimSession                                     7.0.0.0    CimCmdlets
Cmdlet          New-CimSessionOption                               7.0.0.0    CimCmdlets
Cmdlet          Register-CimIndicationEvent                        7.0.0.0    CimCmdlets
Cmdlet          Remove-CimInstance                                 7.0.0.0    CimCmdlets
Cmdlet          Remove-CimSession                                  7.0.0.0    CimCmdlets
Cmdlet          Set-CimInstance                                    7.0.0.0    CimCmdlets

PS ..\pwsh> Get-Command -CommandType Cmdlet | Get-Command |
    Where-Object { $_.Parameters.Count -eq 0 -and $_.ModuleName -eq 'CimCmdlets' }

PS ..\pwsh> 

If you want to get all cmdlets having no parameters but excluding CommonParameters and OptionalCommonParameters you could use:

$common = [System.Management.Automation.PSCmdlet]::CommonParameters
$optional = [System.Management.Automation.PSCmdlet]::OptionalCommonParameters

Get-Command -CommandType Cmdlet | Get-Command | Where-Object {
    -not $_.Parameters.Keys.Where({ -not ($common.Contains($_) -or $optional.Contains($_)) })
}

Or, tip from Mathias, easier way using the CommandMetadata class to get the commands' parameters excluding common and optional:

Get-Command -CommandType Cmdlet | Get-Command | Where-Object {
    -not [System.Management.Automation.CommandMetadata]::new($_).Parameters.Count
}