Call PowerShell script with arguments from another powershell script

Here is something that might help future readers:

foo.ps1:

param ($Arg1, $Arg2)

Make sure to place the "param" code at the top before any executable code.

bar.ps1:

& "path to foo\foo.ps1" -Arg1 "ValueA" -Arg2 "ValueB"

That's it !


After posting the question I stumbled upon the answer. For completeness here it is:

bar.ps1:

#some processing
$ScriptPath = Split-Path $MyInvocation.InvocationName
$args = @()
$args += ("-a", "arg1")
$args += ("-b", 2)
$cmd = "$ScriptPath\foo.ps1"

Invoke-Expression "$cmd $args"

Tags:

Powershell