Passing Commands via Parameter

by Jul 26, 2018

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!