Determine if the current PowerShell Process is 32-bit or 64-bit?

When running a PowerShell script on an x64-bit OS platform, how can you determine in a script which version of PowerShell (32-bit or 64-bit) the script is running?

Background
Both 32-bit and 64-bit versions of PowerShell are installed by default on a 64-bit platform, such as Windows Server 2008. This can lead to difficulties when running the PowerShell script, which must be oriented to a specific architecture (that is, using 64-bit for script for SharePoint 2010 to consume 64-bit libraries).

Related questions:

  • What is the best way to program against powershell volatility x64 and x86? This question is about code that works with both 32-bit and 64-bit architecture. My question is about when you want to make sure that the script only works with the correct version.
+62
scripting 64bit powershell 32-bit
Dec 21 '11 at 11:18
source share
3 answers

If you are working with .NET 4.0 (PowerShell 3.0):

PS> [Environment]::Is64BitProcess True 
+104
Dec 21 '11 at 12:13
source share

To determine in your script which version of PowerShell you are using, you can use the following helper functions (kindly provided by JaredPar the answer to the corresponding question):

 # Is this a Wow64 powershell host function Test-Wow64() { return (Test-Win32) -and (test-path env:\PROCESSOR_ARCHITEW6432) } # Is this a 64 bit process function Test-Win64() { return [IntPtr]::size -eq 8 } # Is this a 32 bit process function Test-Win32() { return [IntPtr]::size -eq 4 } 

The functions above use the fact that the size for System.IntPtr is platform specific. These are 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine.

Note that it is worth noting that the location of the 32-bit and 64-bit versions of Powershell is somewhat misleading. The 32-bit PowerShell is located in C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe , and the 64-bit version of PowerShell is in C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe , courtesy of This article .

+72
Dec 21 '11 at 11:19
source share

You can also use this. I tested it on PowerShell versions 2.0 and 4.0.

 $Arch = (Get-Process -Id $PID).StartInfo.EnvironmentVariables["PROCESSOR_ARCHITECTURE"]; if ($Arch -eq 'x86') { Write-Host -Object 'Running 32-bit PowerShell'; } elseif ($Arch -eq 'amd64') { Write-Host -Object 'Running 64-bit PowerShell'; } 

The value of $Arch will be either x86 or amd64 .

The nice thing about this is that you can also specify a process identifier other than local ( $PID ) to define the architecture of another PowerShell process.

+13
Nov 05 '13 at 18:48
source share



All Articles