Reddy Reddy - 8 months ago 35
PowerShell Question

Powershell calling function with argument as string

I am trying to call

function from custom function. The problem is the arguments to the function can be dynamic.

function Test {
Get-ChildItem $Args

When I try

Test .\ //this works as Path is taken as default argument value
Test .\ -Force //this doesn't work as expected as it still tries to consider entire thing as Path
Test -Path .\ -Force //same error

How to
wrap around function
pass the arguments as it's


$args is an array of arguments, and passing it to the Get-ChildItem wouldn't work, as you've noticed. The PowerShell-way for this would be the Proxy Command.

For a quick-and-dirty hack, you can use Invoke-Expression:

function Test {
    Invoke-Expression "Get-ChildItem $Args"