As an aside: Using plain-text passwords, especially if stored as such in a script, is best avoided in the interest of security.
tl;dr
Start-Process powershell.exe "-File `"$script`"" -Credential $credential
Start-Process
has a -FilePath
parameter that specifies the target executable and an array-typed -ArgumentList
parameter that accepts all arguments to pass to that executable.
These two parameters are the first two positional parameters, so you don't strictly need to name them, but you must fulfill the syntax requirements for -ArgumentList
, which means two things:
If you pass the pass-through arguments individually, you must separate them with ,
(which creates an array).
While not all pass-through arguments require quoting, those with special characters do, which notably includes those that start with -
, because Start-Process
will otherwise interpret them as its parameters.
Thus:
# !! May, but isn't guaranteed to work - see below.
Start-Process powershell.exe '-File', $script -Credential $credential
which is short for:
# !! May, but isn't guaranteed to work - see below.
Start-Process -FilePath powershell.exe -ArgumentList '-File', $script -Credential $credential
Unfortunately, a long-standing bug that won't be fixed in the interest of backward compatibility makes this approach brittle, because PowerShell does not apply on-demand double-quoting and escaping for you when it constructs the process command line behind the scenes - see GitHub issue #5576.
You must therefore perform any embedded double-quoting and escaping yourself, which is ultimately easier to do if you pass a single string to -ArgumentList
that encodes all arguments to pass, using embedded double-quoting and escaping as needed, as shown in the solution at the top.