30

I am trying to create a couple lines of code that will pull from WMI if a machine is either 32/64 bit and then if it is 64 do this .... if it is 32bit do this...

Can anyone help?

1
  • 18
    [Environment]::Is64BitOperatingSystem Commented Aug 13, 2015 at 5:12

7 Answers 7

34

There's two boolean static methods in the Environment you can inspect and compare, one looks at the PowerShell process, one looks at the underlying OS.

if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem) { "PowerShell process does not match the OS" } 
Sign up to request clarification or add additional context in comments.

3 Comments

This variable is not available in older environments, e.g., Powershell 2.0 on Windows 7. @Guvante's answer seems to work.
@dmattp Is64BitProcess and Is64BitOperatingSystem were introduced with .NET 4.0 — first utilized by PowerShell 3.0 — whereas the Win32_OperatingSystem WMI class was introduced with Vista/Server 2008. On older systems one could implement the same logic themselves.
On my 64-bit windows the if always fail, because both condition are True, the -ne should always fail here. We should use -or here.
29

Random discussion about it

Assuming you are running at least Windows 7, the following should work.

Including a sample that worked for me in a 32 bit version of powershell running on a 64 bit machine:

Get-WmiObject win32_operatingsystem | select osarchitecture 

Returns "64-bit" for 64 bit.

if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -eq "64-bit") { #64 bit logic here Write "64-bit OS" } else { #32 bit logic here Write "32-bit OS" } 

8 Comments

Older OSes don't have an osarchitecture property, fwiw.
What are older OS's? We only have windows 7 and up
I think you're good, then. Vista might have been the last one without it.
64-bit isn't always guaranteed, I ran into a user who was getting "64 bits" as the response.
@QuickNull: Can you expand on that comment? This logic can totally fail to detect 64-bit (as mentioned in other comments) but I can't think of a situation where it would falsely detect 64-bit.
|
6

This is similar to a previous answer but will get a correct result regardless of 64-bit/64_bit/64bit/64bits format.

if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -like "64*") { #64bit code here Write "64-bit OS" } else { #32bit code here Write "32-bit OS" } 

Comments

4
[IntPtr]::Size -eq 4 # 32 bit 

The size of an IntPtr will be 4 bytes on a 32 bit machine and 8 bytes on a 64 bit machine (https://msdn.microsoft.com/en-us/library/system.intptr.size.aspx).

3 Comments

how would I put this into and if statement?
This only tells you what version of Powershell you are running. Pulled up a 32 bit window to verify.
"The size of an IntPtr will be 4 bytes on a 32 bit machine and 8 bytes on a 64 bit machine" is incorrect. This code checks if the process, not the operating system, is 32-bit. See the linked documentation where it says (emphasis mine) "The size of a pointer or handle in this process, measured in bytes. The value of this property is 4 in a 32-bit process, and 8 in a 64-bit process."
2
if($env:PROCESSOR_ARCHITECTURE -eq "x86"){"32-Bit CPU"}Else{"64-Bit CPU"} 

-edit, sorry forgot to include more code to explain the usage.

if($env:PROCESSOR_ARCHITECTURE -eq "x86") { #If the powershell console is x86, create alias to run x64 powershell console. set-alias ps64 "$env:windir\sysnative\WindowsPowerShell\v1.0\powershell.exe" $script2=[ScriptBlock]::Create("#your commands here, bonus is the script block expands variables defined above") ps64 -command $script2 } Else{ #Otherwise, run the x64 commands. 

6 Comments

How would I put that into an if statement so i can put my code after 32 bit cpu and after 64 bit cpu. I tried your line with no luck, although I am sure there is more to is
This returns true in a 32 bit version of powershell running on a 64 bit version of Windows.
Apologies, forgot to include the code for dealing with running in x64 or x86 powershell.
Will this be fine running on newer versions of windows ex.8.1
I don't think there would be any problems, as long as the Powershell environment variable $env:PROCESSOR_ARCHITECTURE was available.
|
2

Two lines smashed togther for a nice one-liner:

Write-Host "64bit process?:"$([Environment]::Is64BitProcess) ;Write-Host "64bit OS?:"$([Environment]::Is64BitOperatingSystem); 

Comments

-2

It is never necessary to filter a Boolean value (such as the value returned by the -Eq operator) through an “If” or to compare a Boolean value or expression to $True or $False.

Jose's one-liner simplifies to:

$global:Is64Bits=(gwmi win32_operatingsystem | select osarchitecture).osarchitecture -eq "64-bit" 

1 Comment

This doesn't work. What's "gwmi"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.