Invoke-PowerShell

Invokes a script block in a separate powershell.exe process.

Syntax

Invoke-PowerShell -ScriptBlock <ScriptBlock> [-ArgumentList <Object[]>] [-OutputFormat <String>] [-x86] [-Runtime <String>] [<CommonParameters>]
Invoke-PowerShell -FilePath <String> [-ArgumentList <Object[]>] [-OutputFormat <String>] [-ExecutionPolicy {Unrestricted | RemoteSigned | AllSigned | Restricted | Default | Bypass | Undefined}] [-x86] [-Runtime <String>] [<CommonParameters>]

Description

If using PowerShell v2.0, the invoked PowerShell process can run under the .NET 4.0 CLR (using v4.0 as the value to the Runtime parameter).

If using PowerShell v3.0, you can only run script blocks under a v4.0 CLR. PowerShell converts script blocks to an encoded command, and when running encoded commands, PowerShell doesn't allow the -Version parameter for running PowerShell under a different version. To run code under a .NET 2.0 CLR from PowerShell 3, use the FilePath parameter to run a specfic script.

This function launches a PowerShell process that matches the architecture of the operating system. On 64-bit operating systems, you can run under 32-bit PowerShell by specifying the x86 switch).

PowerShell's execution policy has to be set seperately in all architectures (i.e. x86 and x64), so you may get an error message about script being disabled. Use the -ExecutionPolicy parameter to set a temporary execution policy when running a script.

Parameters

Name Type Description Required? Pipeline Input Default Value
ScriptBlock ScriptBlock

The command to run.

true false
FilePath String

The script to run.

true false
ArgumentList Object[]

Any arguments to pass to the command/scripts.

false false
OutputFormat String

Determines how output from the PowerShel command is formatted

false false
ExecutionPolicy ExecutionPolicy

The execution policy to use when running a script. By default, execution policies are set to Restricted. If running an architecture of PowerShell whose execution policy isn't set, Invoke-PowerShell will fail.

false false
x86 SwitchParameter

Run the x86 (32-bit) version of PowerShell, otherwise the version which matches the OS architecture is run, regardless of the architecture of the currently running process.

false false False
Runtime String

The CLR to use. Must be one of v2.0 or v4.0. Default is the current PowerShell runtime.

false false

EXAMPLE 1

Invoke-PowerShell -Command { $PSVersionTable }

Runs a separate PowerShell process which matches the architecture of the operating system, returning the $PSVersionTable from that process. This will fail under 32-bit PowerShell on a 64-bit operating system.

EXAMPLE 2

Invoke-PowerShell -Command { $PSVersionTable } -x86

Runs a 32-bit PowerShell process, return the $PSVersionTable from that process.

EXAMPLE 3

Invoke-PowerShell -Command { $PSVersionTable } -Runtime v4.0

Runs a separate PowerShell process under the v4.0 .NET CLR, returning the $PSVersionTable from that process. Should return a CLRVersion of 4.0.

EXAMPLE 4

Invoke-PowerShell -FilePath C:\Projects\Carbon\bin\Set-DotNetConnectionString.ps1 -ArgumentList '-Name','myConn','-Value',"'data source=.\DevDB;Integrated Security=SSPI;'"

Runs the Set-DotNetConnectionString.ps1 script with ArgumentList as arguments/parameters.

Note that you have to double-quote any arguments with spaces. Otherwise, the argument gets interpreted as multiple arguments.

EXAMPLE 5

Invoke-PowerShell -FilePath Get-PsVersionTable.ps1 -x86 -ExecutionPolicy RemoteSigned

Shows how to run powershell.exe with a custom executin policy, in case the running of scripts is disabled.