Thursday, November 15, 2012

Launching a PowerShell script in 64 bit mode on a 64 bit machine

If a 32-bit process launches PowerShell on a 64 bit machine, it will launch powershell.exe from the 32 bit system32 folder rather than the 64 bit one. Some scripts need to run in 64 bit mode, so the solution is to have the script relaunch itself in 64 bit mode if it detects that it has been run in 32 bit mode. This is one way to do it:
$powerShellExe = join-path $env:WINDIR SysNative\WindowsPowerShell\v1.0\powershell.exe
if(test-path ($powerShellExe)) {
    if([System.Runtime.InterOpServices.Marshal]::SizeOf([System.IntPtr]) -eq 4) {
        write-host "Re-launching script in native 64 bit mode..."
        & $powerShellExe $MyInvocation.MyCommand.Definition $PSBoundParameters.Values


Post a Comment