2
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

2
  • 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. Commented Jan 6, 2021 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 or higher, but note its other effects. See this answer for more information. Commented Jan 6, 2021 at 22:14

1 Answer 1

3

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.