1
function test([string]$word){
return $word
}

Get-ADUser -Identity Function:\test("bill")

From what I can tell the above should work. MS has an example below where it looks like they call a function as a parameter. But its not working for me.

MS example.

Get-ChildItem -Path Function:\Get-*Version | Remove-Item

Link - https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/09-functions?view=powershell-7.1

yazmnh87
  • 316
  • 5
  • 12
  • 2
    you will need to enclose the func AND its parameter[s] in parens to force the func to evaluate BEFORE the value is passed to the calling func/cmdlet. – Lee_Dailey Jan 06 '21 at 22:12
  • As an aside: PowerShell functions, cmdlets, scripts, and external programs must be invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo('arg1', 'arg2')`. If you use `,` to separate arguments, you'll construct an _array_ that a command sees as a _single argument_. To prevent accidental use of method syntax, use [`Set-StrictMode -Version 2`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher, but note its other effects. See [this answer](https://stackoverflow.com/a/65208621/45375) for more information. – mklement0 Jan 06 '21 at 22:14

1 Answers1

2

To pass a command's / expression's output as a command argument, use (...), the grouping operator:

Get-ADUser -Identity (test "bill")

As for the Function:\test syntax: you can only use that for retrieving function definitions, not for direct invocation.

The syntax relies on the Function: PowerShell drive, which provides access to all function definitions - see the about_Providers conceptual help topic.

For the sake of completeness (there's no good reason to take this approach here): Using namespace variable notation and &, the call operator, you could leverage the Function:\test path syntax - albeit without the \ - as follows:

# Same as above.
Get-ADUser -Identity  (& $Function:test "bill")
mklement0
  • 382,024
  • 64
  • 607
  • 775