How to use powershell.exe with -Command using a scriptblock and parameters
<p>For reasons that should not impact the current question I need to run a script, with the definition and parameters outside the command, inside a different PowerShell instance, without using PSSessions, background jobs or files (I have working examples for PSSession, background jobs and .ps1 files and I'm aware they can replace what I'm trying to do, but I need a working example with <code>powershell.exe -Command</code> as well).</p>

<p>I looked at the <a href="https://msdn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help" rel="noreferrer">help for <code>powershell.exe</code></a>, and it should support what I am trying to do, but I can't get it working with all that I need (script definition and parameters outside the command).</p>

<p>As a working example I have:</p>

<pre><code>$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command {Invoke-Command -ScriptBlock {
    param($a1,$a2)
    $a1*6
    $a2*5} -Argumentlist @(8,'abc')}
</code></pre>

<p>I need to be able to at least move the <code>-ArgumentList</code> outside the command, like:</p>

<pre><code>$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command {Invoke-Command -ScriptBlock {
param($a1,$a2)
$a1*6
$a2*5} -Argumentlist @($args[0],$args[1])} -args @(8,'abc')
</code></pre>

<p>and even better have:</p>

<pre><code>$script={
param($a1,$a2)
$a1*6
$a2*5}
$args=@(8,'abc')
$abc = powershell.exe -WindowStyle Hidden -NonInteractive -Command $script -args $args
</code></pre>

<p>I already looked at the following similar questions, but couldn't find what I needed:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/13724940/how-to-run-a-powershell-script-from-the-command-line-and-pass-a-directory-as-a-p">How to run a Powershell script from the command line and pass a directory as a parameter</a></li>
<li><a href="https://stackoverflow.com/questions/39828984/how-can-i-start-process-powershell-exe-with-some-string-splitter">How can I Start-Process powershell.exe with some string splitter?</a></li>
</ul>