views:

18

answers:

1

How can I find or list PowerShell's -as family of parameters?

So far I found -as Type e.g. -as[Int] Also I have found -asHashTable

My questions is what other -as parameters are available?

+1  A: 

This might probably have a more elegant solution if one put any effort into it:

PS Home:\> gcm | where { $_.CommandType -eq 'Cmdlet' } | foreach { $_ | Add-Member -PassThru NoteProperty AsParameters ($_.Parameters.GetEnumerator() | ? {$_.Key -cmatch '^As([A-Z]|$)'} | % { $_.Key }) } | where { $_.AsParameters } | select Name,AsParameters

Name                   AsParameters
----                   ------------
ConvertTo-Html         As
ConvertTo-SecureString AsPlainText
ConvertTo-Xml          As
Export-Alias           As
Get-EventLog           {AsBaseObject, AsString}
Get-Unique             AsString
Get-WmiObject          AsJob
Group-Object           {AsHashTable, AsString}
Import-Module          AsCustomObject
Invoke-Command         AsJob
Invoke-WmiMethod       AsJob
New-Module             AsCustomObject
Read-Host              AsSecureString
Remove-WmiObject       AsJob
Restart-Computer       AsJob
Set-WmiInstance        AsJob
Stop-Computer          AsJob
Test-Connection        AsJob

Code dissected:

# find all commands
Get-Command |
  # that are cmdlets (exclude aliases or functions)
  Where-Object { $_.CommandType -eq 'Cmdlet' } |
  ForEach-Object {
    # Add another property that contains all parameters
    # that starts with 'As' – I use a regex here to exclude
    # parameters like -Assembly
    $_ | Add-Member -PassThru NoteProperty AsParameters (
      $_.Parameters.GetEnumerator() |
        Where-Object { $_.Key -cmatch '^As([A-Z]|$)' } |
        ForEach-Object { $_.Key }
    )
  } |
  # Exclude commands that don't have parameters that start with 'As'
  Where-Object { $_.AsParameters } |
  Select-Object Name,AsParameters
Joey
A wonderful script. I can see now that it's Group-Object that has the -AsHashTable parameter. Also Get-Eventlog has an interesting parameter called -AsBaseObject
Guy Thomas