Here is a rather unusual use case for function parameters: a user can pass an output command:

function Get-ProcessList
{
  param
  (
    [string]
    [ValidateSet('GridView','String')]
    $OutputMode = 'String'
  )
  
  Get-Process | & "Out-$OutputMode"
  
}

# output as a string
Get-ProcessList -OutputMode String
# output in a grid view window
Get-ProcessList -OutputMode GridView

Twitter This Tip! ReTweet this Tip!

Anonymous
  • This is a great example of the dynamic nature of Powershell. Great tip!

  • Why would you do this? The below commands do the same thing and follow the principle of pipeline chaining commands:

    Get-Process | Out-String

    Get-Process | Out-GridView

    Due to the output template of Get-Process, by writing the function this way you will not be able to add the other properties of Get-Process to the output. For example, if you wanted to get the username for the process by adding the IncludeUserName switch.

    If you are simply trying to save keystrokes, use aliases:

    ps|ogv