≡ Menu

Get Operating System architecture(32-bit or 64-bit) using powershell

Updated on 2/13/2012 with a more robust function to get OS architecture and details.

After introduction of windows 7 and 2008, use of 64-bit operating systems have increased. Today I came across a requirement to generate list of servers which are running with 32-bit operating system and 64-bit operating system. I look at OperatingSystem attribute value of active directory objects, but that doesn’t have sufficient information. I did bit more research and came-up with below powershell script which works for windows XP, 2003, 7, 2008.

Code:

function Get-OSArchitecture {            
[cmdletbinding()]            
param(            
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]            
    [string[]]$ComputerName = $env:computername                        
)            

begin {}            

process {            

 foreach ($Computer in $ComputerName) {            
  if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {            
   Write-Verbose "$Computer is online"            
   $OS  = (Get-WmiObject -computername $computer -class Win32_OperatingSystem ).Caption            
   if ((Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer -ea 0).OSArchitecture -eq '64-bit') {            
    $architecture = "64-Bit"            
   } else  {            
    $architecture = "32-Bit"            
   }            

   $OutputObj  = New-Object -Type PSObject            
   $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()            
   $OutputObj | Add-Member -MemberType NoteProperty -Name Architecture -Value $architecture            
   $OutputObj | Add-Member -MemberType NoteProperty -Name OperatingSystem -Value $OS            
   $OutputObj            
  }            
 }            
}            

end {}            

}

Usage:

Get-OSArchitecture -ComputerName PC1, PC2, PC3 | ft -auto

Hope this helps..

{ 23 comments… add one }
  • supi007 April 19, 2011, 3:16 pm

    Hi,

    On Windows Server 2003, the (get-wmiobject win32_operatingsystem).Caption command won’t give you any relevant information about the OS architecture. Unfortunately.
    I tried it on a Server 2003 Std 32-bit and I got this: “Microsoft(R) Windows(R) Server 2003, Standard Edition”. I tried on Server 2003×64 as well and I got this: “Microsoft(R) Windows(R) Server 2003 Standard x64 Edition”. So I would not say that it is a right solution. Until now I didn’t find the right way how could I determine the architecture of my Server 2003.
    On Server 2008Sp1 and R2 I use the (get-wmiobject win32_operatingsystem).OSArcitecture. It works properly. I recommend it.

  • Sitaram Pamarthi April 19, 2011, 11:25 pm

    @supi007, Whatever you said is correct. However, the code “(get-wmiobject win32_operatingsystem).Caption” returning is in fact having the version of Operating System. In case of 64-bit OS, you have in the returned value. In case of 32-bit, the returned value will not contain either 64 or 32-bit indication which means by default it is 32-bit. Only thing you need to do is, parse the code output and mark it as 64-bit if “x64” is found in string, otherwise it is 32-bit.

    Hope this explanation helps.

  • Dave D May 20, 2011, 11:54 pm

    A simpler method is to look at the address width as follows:
    $Processor = Get-WmiObject Win32_Processor -ComputerName SERVER -namespace rootcimv2 | where {$_.DeviceID -eq “CPU0”} | select AddressWidth

    $Processor.AddressWidth will return 32 or 64, which will tell you what architecture it is. Limiting it to “CPU0” makes sure you get just one 32 or 64 no matter how many processors there are. Works for Windows 2000 and up.

  • Mark June 24, 2011, 9:15 pm

    @Dave D –

    The original example reports the OS architecture… Unless I misunderstand, you’re reporting the Processor architecture. They may not be the same, since you can run 32-bit Windows on 64-bit processors.

    There are cases where you might want to know either piece of information, but they’re not necessarily the same thing.

  • Mark June 24, 2011, 9:24 pm

    Actually, I was wrong… looks like Dave D’s method will return the OS architecture correctly.

    Back to lurking…

  • Krishna November 23, 2011, 8:09 pm

    All of them are working properly. However, i would like to know if we can get the version of OS if it is R2. When i run this for Windows 2003 R2, it doesnt show up in the output.

    Can some check this ?

    • Colin September 9, 2012, 9:44 am

      try Get-WmiObject -Class Win32_OperatingSystem | Select OtherTypeDescription

      that property will only be populated if running Server 2003 R2.

  • Shubham Rajvanshi August 17, 2012, 11:50 pm

    Function gives random result,for 2003 gave 2008 and for some 32 bit gave 64 bit

  • Ray September 4, 2012, 9:30 am

    great work. thanx a lot for the function.

  • Colin September 9, 2012, 9:48 am

    what about something like this inside your For-Each loop?

    $Architecuture = ($env:PROCESSOR_ARCHITECTURE)

  • kokotas September 21, 2012, 12:09 pm

    Maybe this will work on a wider variety of occasions:
    (Get-WmiObject -Class Win32_ComputerSystem).SystemType -match ‘(x64)’

  • Leonard Hopkins March 4, 2014, 11:44 pm

    Get absolutely nothing when I run this script except a return to the CLI. What am I missing?

    • Sitaram Pamarthi March 5, 2014, 7:31 pm

      Its a PS function. So after you copy paste, it will be obviously return to CLI. That completes the function import operation. To use it just call the function from the window.

      Get-OSArchitecture -ComputerName PC1, PC2, PC3 | ft -auto

  • Leonard Hopkins March 5, 2014, 2:13 am

    Don’t you have something that can query AD by OU and then pipe it to the script. I am new to PowerShell and I get that the reason I came up blank on running it was because I am suppose to supply the list of systems. However, that is the whole point, who knows what is out there? I need to know how to extract the systems in our organization and have this script execute against it. How can that be done?

    • Sitaram Pamarthi March 5, 2014, 7:36 pm

      Get-OSArchitecture -ComputerName (get-content c:\temp\servers.txt)

      Keep all your severs in servers.txt before you execute the above

  • clarksss June 27, 2014, 11:38 pm

    How would i return a value from that function?
    I have tried [ref]$value = $architecture, but having no luck

    any ideas?

    • clarksss June 27, 2014, 11:40 pm

      I want to use that return value (or object.value) as input to another function.

      • clarksss June 28, 2014, 12:45 am

        sorry it was this simple: (new to powershell)
        PS H:\> $OSArch = Get-OSArchitecture -ComputerName Computer
        PS H:\> $OSArch.architecture
        64-Bit

  • PS Jedi Apprentice January 13, 2016, 5:04 am

    I am still learning the ways of Powershell, and am looking for some help modifying the $outputfile. I am able to get the script to read a list of computers (ComputerName is the header) from a csv, but I would like for the output to be a separate file for each computer. I will be running this against 1,000’s of workstations and find it to be easier to look for one small file compared to comping through one potentially very large file.

    Hopefully someone is still watching this thread and can reply. Either way, thank you for this excellent write-up!

  • Raul Rodriguez April 20, 2016, 3:59 am

    Working perfect, thanks for sharing.

Leave a Comment