Tuesday, June 14, 2011

How do detect operating system architecture with PowerShell

Two weeks ago when I wrote Configuration Manager Servers Health State script, then I thought how is the best way to detect operating system architecture. In this post I will point out three different ways how to detect it. Starting from Windows Vista it is much easier, but if you have still Windows XP or Server 20003 operating systems in your environment, then you have to query it from other WMI class.

Method 1

Some PowerShell scripters uses [IntPtr]::Size to detect operating system architecture. If you get [intPtr]::Size 4, then operating system architecture is 32 bit and if you get this number 8, then it is 64 bit then operating system. You can read more about IntPtr from here: http://msdn.microsoft.com/en-us/library/system.intptr.size.aspx

if([IntPtr]::Size -eq 4){                        
Write-Host "32 bit operating system"
}
Else{
Write-Host "64 bit operating system"
}
1

Method 2

If you have only Windows Vista, 7 and server 2008 in your environment, then you can use Win32_OperatingSystem WMI class OSArchitectur property to query operating system architecture. You can read more about Win32_OperatingSystem WMI class from here http://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx

Get-WmiObject -Class Win32_OperatingSystem | Select-Object OSArchitecture

2 

Method 3

I use this method to detect operating system architecture in Windows XP and Server 2003, but you can use it on Windows Vista and 7 too. In Windows XP and Server 2003 there is no such property like OSArchitectur. In such a case you have to use different WMI class to detect operating system architecture. You can use Win32_Processor WMI class to detect it and you have to query AddressWidth property. If you get query result 32, then it is 32 bit operating system and 64 is 64 bit operating system

Get-WmiObject -Class Win32_Processor | Select-Object AddressWidth

  3

Bonus

If you want to know that is it possible to upgrade that computer to 64 bit operating system then you can use same WMI Win32_Processor class to detect it. You have to query DataWidth property. If you get AddressWidth 32 and DataWidth 64, then it is possible to upgrade that computer to 64 bit operating system.

$Query = Get-WmiObject -query "Select AddressWidth, DataWidth,Architecture from Win32_Processor"            

switch ($Query.Architecture)
{
0 {$Architecture = "x86"}
1 {$Architecture = "MIPS"}
2 {$Architecture = "Alpha"}
3 {$Architecture = "PowerPC"}
6 {$Architecture = "Itanium"}
9 {$Architecture = "x64"}
Default {"Unknown"}
}

If($Query.AddressWidth -eq $Query.DataWidth){

Write-Host "$($Query.AddressWidth) Bit Operating system"

}
Else{

Write-Host "$($Query.AddressWidth) Bit Operating system is upgradable to 64 bit"
}

4


Conclusion


I recommend to use method 2 if you have only Windows Vista, 7 and Server 2008 operating systems in your environment.

1 comment:

  1. [IntPtr]::Size only works when used in the 64-bit version of powershell. When used in the x86 version of powershell on a 64-bit OS it gives the same value as on a 'normal' 32-bit OS.

    ReplyDelete