42

I know it's a weird question but I am locked into a third party vendor which launches a 32-bit cmd.exe on a target 64-bit Windows Server 2008 R2 clustered server. From here I want to launch a 64-bit PowerShell window and run a script.

Here's my test:

powershell.exe "Get-Module -ListAvailable| Where-Object {$_.name -eq 'FailoverClusters'}"

If I run this from a 32-bit cmd.exe I get nothing returned. If I run from a 64-bit cmd.exe I get:

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Manifest   FailoverClusters          {}

Any ideas on what I can do to invoke a 64-bit powershell script from a 32-bit cmd shell?

Mark Allison
  • 6,838
  • 33
  • 102
  • 151

2 Answers2

103

syswow64 lets you run 32 bit system executables from 64 bit code. sysnative lets you run 64 bit system executables from 32 bit code.

So, you need to run:

%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
Jason Shirk
  • 7,734
  • 2
  • 24
  • 29
  • 1
    Just beat me to the punch. :-) – Keith Hill Sep 27 '13 at 16:44
  • 6
    I have Windows 7 Enterprise 64-bit and I get "The system cannot find the path specified" if I use this. So the line that works for me to run 64 bit powershell is this: %systemroot%\syswow64\windowspowershell\v1.0\powershell.exe – Sudhanshu Mishra Jul 21 '14 at 08:40
  • 17
    @mishrud is mistaken. Calling `%systemroot%\syswow64\windowspowershell\v1.0\powershell.exe` from 32-bit cmd.exe gets you the 32-bit powershell. You can verify this using `ls env:` The 32-bit version will show `PROCESSOR_ARCHITECTURE` as `x86` Moreover, `syswow64` contains only 32-bit equivalents of 64-bit DLLs and EXEs. – Donal Lafferty Sep 25 '14 at 08:50
  • 3
    It works for me on Win10, but I noticed tab completion doesn't work on sysnative. – Jason Shirk Aug 05 '15 at 04:59
  • The answer is very useful. I'm here the second time. Really would like to vote up the second time. – Andrzej Martyna May 11 '16 at 12:59
  • 2
    Note that this command only works when run from a 32-bit process on a 64-bit system. it will not work if you try to run this from a 64-bit process. The sysnative redirector is only available to 32-bit processes. – Nikhil Dabas Jul 12 '16 at 12:00
  • 2
    I'm using Windows 10, I have faced the issue with installing 64bit module. I do not find "sysnative" direcotry. So i've tried using Pwershell.exe found in the path `"C:\Windows\System32\WindowsPowerShell\v1.0"`. Able to successfully install 64bit module using this exe. – Saravanan G Sep 05 '17 at 11:21
  • This answer didn't work for me. The sysnative bit proved the problem. This answer worked though. https://stackoverflow.com/a/6872000/852806 – JsAndDotNet Nov 21 '17 at 13:58
  • Yeah, magic I guess, see the official docs: https://learn.microsoft.com/en-us/windows/desktop/WinProg64/file-system-redirector – Jason Shirk Sep 10 '18 at 17:52
  • This `Sysnative` is magic. Therefore, if you are in a 32-bit PowerShell, you can launch a new window with a 64-bit PowerShell with `Start-Process $env:windir\Sysnative\WindowsPowerShell\v1.0\powershell.exe` - or you can switch to a 64-bit PowerShell in the same window with `& $env:windir\Sysnative\WindowsPowerShell\v1.0\powershell.exe` like in the other answer by _ProfessionalAmateur_. – Jeppe Stig Nielsen Mar 13 '23 at 12:12
26

This script will check as see what version of powershell you are running and will relaunch itself to 64-bit if you are running in 32-bit. When the relaunch occurs it will also pass in any parameters used in the original call.

#############################################################################
#If Powershell is running the 32-bit version on a 64-bit machine, we 
#need to force powershell to run in 64-bit mode .
#############################################################################
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
    write-warning "Y'arg Matey, we're off to 64-bit land....."
    if ($myInvocation.Line) {
        &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
    }else{
        &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
    }
exit $lastexitcode
}


write-host "Main script body"

#############################################################################
#End
#############################################################################    
ProfessionalAmateur
  • 4,447
  • 9
  • 46
  • 63
  • You saved my bacon! I could not execute IIS powershell cmdlets through the config management tool SaltStack's powershell shell, and it turned out it was executing them in 32 bit (!!) and IIS required 64. Appending this to my scripts saved it. Very excellent. – Mr.Budris Jun 15 '17 at 14:44
  • Nice that my 11 year old code is still alive. You can upvote it here, too: https://stackoverflow.com/a/8749393/1133043 – ggz Feb 01 '21 at 20:13
  • This helped me! I faced the problem when trying to execute a PowerShell script through the azure DevOps PS task. I wanted the script to be executed on the 64-bit instance. The self-hosted agent had two PowerShell instances, 32 and 64 bit, and the DevOps task, whatever I did, ended up using the 32 bit Powershell. – Meer Dec 22 '21 at 13:17