yazmnh87
yazmnh87

Reputation: 326

Calling function as parameter in powershell cmdlet

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

Upvotes: 2

Views: 1258

Answers (1)

mklement0
mklement0

Reputation: 437698

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")

Upvotes: 3

Related Questions