Search code examples
powershellinvoke-command

How to convert $args to a string and execute?


I'd like to pass all arguments that were given to the script and execute.

For example, given execute.ps1 script:

Invoke-Expression $($args[0])

I can run:

.\execute.ps1 hostname
myhostname

Same with two parameters with this script:

Invoke-Expression "$($args[0]) $($args[1])"

and executing it by:

.\execute.ps1 echo foo
foo

How I can make the script universal to support unknown number of arguments?

For example:

.\execute.ps1 echo foo bar buzz ...

I've tried the following combinations which failed:

Invoke-Expression $args

Invoke-Expression : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Command'. Specified method is not supported.


Invoke-Expression [system.String]::Join(" ", $args)

Invoke-Expression : A positional parameter cannot be found that accepts argument 'System.Object[]'.


Invoke-Expression $args.split(" ")

Invoke-Expression : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Command'. Specified method is not supported.


Invoke-Expression [String] $args

Invoke-Expression : A positional parameter cannot be found that accepts argument 'System.Object[]'.


Solution

  • I recommend Bill_Stewart's answer to avoid issues with the command itself (first argument) having spaces. But even with that answer, you would have to be careful to individual quote arguments, with the caveat that that itself itself a complicated thing.


    You can just do:

     Invoke-Expression "$args"
    

    By default converting to a string that way will join it with spaces (technically, join it with the default output field separator, which is the value of $OFS, which defaults to a space).

    You can also do a manual join as in Wayne's answer.