Making sure PowerShell scripts run in 32-bit

by May 3, 2011

If you are using code that can only run in a 32-bit environment (i.e. using old database drivers or COM objects), here is a solution that will re-launch the script in a 32-bit PowerShell when it is launched in a 64-bit environment:

if ($env:Processor_Architecture -ne "x86")   
{ write-warning 'Launching x86 PowerShell'
&"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noninteractive -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass
exit
}
"Always running in 32bit PowerShell at this point."
$env:Processor_Architecture
[IntPtr]::Size

 

Twitter This Tip!
ReTweet this Tip!