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?
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" } 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.if always fail, because both condition are True, the -ne should always fail here. We should use -or here.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" } 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" } [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).
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. $env:PROCESSOR_ARCHITECTURE was available.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"
[Environment]::Is64BitOperatingSystem