Running Portions of Code in 32-bit or 64-bit

by May 30, 2013

To execute code in 32-bit from within a 64-bit environment (or vice versa), you can create appropriate aliases:

In a 32-bit PowerShell, you create:

Set-Alias Start-PowerShell64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe"

And in a 64-bit PowerShell, you would create:

Set-Alias Start-PowerShell32 "$env:windir\syswow64\WindowsPowerShell\v1.0\powershell.exe"

Now, it is simple to run code (and receive results).

The next example runs in a 64-bit PowerShell (as a proof, pointer sizes are 8). When you run the code in a 32-bit PowerShell, you get a pointer size of 4 which again proves that your code indeed is running in a 32-bit environment:

PS> [IntPtr]::Size
8

PS> Start-PowerShell32 { [IntPtr]::Size }
4

Note that the alias will return rich (serialized) objects that you can process as usual:

PS> Start-PowerShell32 { Get-Service } | Select-Object -Property DisplayName, Status

This works because when you submit your code to powershell.exe as a script block rather than plain text, powershell.exe returns serialized objects instead of plain text.

Twitter This Tip! ReTweet this Tip!