1

I have a script that needs to run in 32bit powershell. I want to, at run time, determine if it is running in 32 or 64 bit powershell, and if 64bit, switch dynamically to 32bit.

Basically the exact opposite of this.

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
}

But for some reason I can not figure out how to reverse this. Please help.

gilbertbw
  • 634
  • 2
  • 9
  • 27
Thomas
  • 193
  • 1
  • 6
  • 14
  • You're expecting us to leave this site and go somewhere else in order to see what you're asking, which is not how this site works. Post the code you've tried that isn't working for you here, in the question itself, and then ask a specific question related to that code. – Ken White May 24 '17 at 17:43
  • You might find [this StackOverflow question](https://stackoverflow.com/questions/8588960/determine-if-current-powershell-process-is-32-bit-or-64-bit) useful. – Jeff Zeitlin May 24 '17 at 17:46
  • @KenWhite I was not aware this was against the rules. I am sorry to have inconvenienced you so greatly. – Thomas May 24 '17 at 17:48
  • Aside from that, what problem are you trying to solve by doing this? – Bill_Stewart May 24 '17 at 18:11
  • I am using Microsoft.Jet.OLEDB.4.0 which requires a 32bit environment. I am performing the check at the beginning of the script to automatically switch to the appropriate environment (if it is not already). – Thomas May 25 '17 at 11:57

2 Answers2

3

The 32-bit version of PowerShell on a 64-bit system resides in

$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe  

So test if running on a 64-bit system:

if ([Environment]::Is64BitProcess) {
  write-warning "Switching to 32-bit.PowerShell."
  if ($myInvocation.Line) {
    &"$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe" -Non -NoP -NoL $myInvocation.Line
  }else{
    &"$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe" -Non -NoP -NoL -file "$($myInvocation.InvocationName)" $args
  }
  exit $lastexitcode
}
0
if([IntPtr]::size -eq 8) {
    Write-Host "Switching to 32bit..."
    if ($myInvocation.Line) {
        &"$env:WINDIR\SysWOW64\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
    }else{
        &"$env:WINDIR\SysWOW64\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
    }
    exit $lastexitcode
}
Thomas
  • 193
  • 1
  • 6
  • 14