none
How can I assemble and pass named parameter lists to a function? RRS feed

  • Question

  • I'd like to assemble a list of named parameters based on certain conditions and pass them to the target function.

    This simple example does not work:

        $arg = '-Recurse'
        Get-ChildItem $arg

    because Get-ChildItem does not recognize '-Recurse' as a named parameter.

    Obviously, [string] is not appropriate for specifying parameter names.

    But what type does $arg need to be and how do I add the parameter names and values?

    Sunday, August 28, 2016 8:00 AM

Answers

  • If parameter is a switch
    Parameter name = $true
    ex @{recurse = $true}

    And for parameter which accepts a value
    Parameter Name = 'Value'
    ex @{include = '*.csv'}

    $arg = @{
        recurse=$true
        Include='*.csv'
    }
    Get-ChildItem @arg

    • Marked as answer by Frank Heimes Sunday, August 28, 2016 6:30 PM
    Sunday, August 28, 2016 8:49 AM

All replies

  • $arg = @{recurse=$true}
    Get-ChildItem @arg

    Sunday, August 28, 2016 8:41 AM
  • If parameter is a switch
    Parameter name = $true
    ex @{recurse = $true}

    And for parameter which accepts a value
    Parameter Name = 'Value'
    ex @{include = '*.csv'}

    $arg = @{
        recurse=$true
        Include='*.csv'
    }
    Get-ChildItem @arg

    • Marked as answer by Frank Heimes Sunday, August 28, 2016 6:30 PM
    Sunday, August 28, 2016 8:49 AM
  • HELP ABOUT_SPLATTING

    \_(ツ)_/

    Sunday, August 28, 2016 1:33 PM
  • Thanks to all of you!

    This is actually the first time I heard about Powershell "splatting".

    Sunday, August 28, 2016 6:32 PM