Invokes a script block in a separate powershell.exe process.
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>]
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.
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 |
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 |
false | false |
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.
Invoke-PowerShell -Command { $PSVersionTable } -x86
Runs a 32-bit PowerShell process, return the $PSVersionTable from that process.
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
.
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.
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.