Reddy Reddy - 1 year ago 163
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

Answer Source

$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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download