PowerShell "get-command"

If you've spent any time in the PowerShell, then you're probably familiar with get-command (gcm). Here's an example I threw together taking that to the next level. This will help you when you can't remember the command you want to use. Say you want to find all shell commands that deal with DSNs but you […]

If you've spent any time in the PowerShell, then you're probably familiar with get-command (gcm). Here's an example I threw together taking that to the next level. This will help you when you can't remember the command you want to use.

Say you want to find all shell commands that deal with DSNs but you can't remember the commands that deal with DSNs...

[PS] C:\>findparams *DSN* 
New-SystemMessage 
--------- 
DsnCode

Set-ForeignConnector 
--------- 
RelayDsnRequired

Set-TransportConfig 
---------

GenerateCopyOfDSNFor 
Set-TransportServer 
--------- 
ExternalDelayDsnEnabled 
ExternalDsnDefaultLanguage 
ExternalDsnLanguageDetectionEnabled 
ExternalDsnMaxMessageAttachSize 
ExternalDsnReportingAuthority 
ExternalDsnSendHtml 
InternalDelayDsnEnabled 
InternalDsnDefaultLanguage 
InternalDsnLanguageDetectionEnabled 
InternalDsnMaxMessageAttachSize 
InternalDsnReportingAuthority 
InternalDsnSendHtml

Please note that it only returns "new, set, and add" commands. "get" commands do not return the parameters in the self-documenting help. But if New-SystemMessage takes "DsnCode", you can assume that Get-SystemMessage will return it, or now that you know you want that you can do this:

[PS] C:\>gcm -noun systemmessage 
CommandType     Name                            Definition 
-----------     ----                            ---------- 
Cmdlet          Get-SystemMessage               Get-SystemMessage [[-Identit... 
Cmdlet          New-SystemMessage               New-SystemMessage -DsnCode <... 
cmdlet          remove-systemmessage            remove-systemmessage [-ident... 
cmdlet          set-systemmessage               set-systemmessage [-identity...

Ok, here's the nifty function you'll need to make this work.

param([string]$arg) 
function findparams ($n) 
{ 
      $cmdarray = get-command | ? { ($_.parametersets| %{$_.parameters } |%{$_.name}) -like $n }; 
      foreach ($cmd1 in $cmdarray) 
      { 
            $cmd1.name; 
            "---------"; 
            ($cmd1.parametersets| %{$_.parameters } |%{$_.name}) -like $n | sort -unique; 
            "" 
      } 
} 
findparams($arg)

Exchange Team Blog

Scripting, Exchange Server 2007, PowerShell, Tips and Tricks, Microsoft