Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Test 64-Bit Operating System

Posted on May 16, 2013

One of the great features of PowerShell is how much you can get from a relatively simple one line command. For example. you might want to test if a computer is running a 64-bit operating system. You can find out with a command as simple as this.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!
PS C:\> (get-wmiobject win32_operatingsystem -comp chi-dc01).OsArchitecture -match "64"
True
PS C:\> (get-wmiobject win32_operatingsystem -comp novo8).OsArchitecture -match "64"
False

If you are running PowerShell 3 you could substitute Get-CimInstance. One thing to be aware of with this particular class is that the OSArchitecture property isn't valid on older operating systems like Windows Server 2003. You'll get an exception.

osarchitecture-fail

In this case I modified the WMI query to only return the OSArchitecture property, which doesn't exist. Otherwise I would just get false which might not be entirely true. Of course, I always get carried away so before I knew it I had turned this one line command into a function.

#requires -version 2.0

Function Test-Is64Bit {
[cmdletbinding()]

Param(
[ValidateNotNullorEmpty()]
[string]$Computername=$env:computername,
[Alias("RunAs")]
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
)

Try {
  #hash table of parameters to splat
  $wmiParams = @{
    Class = 'win32_operatingsystem'
    Property = 'osarchitecture'
    ComputerName = $computername
    ErrorAction = 'Stop'
  }
  if ($credential) {
    Write-Verbose "Adding credential"
    $wmiParams.Add("Credential",$Credential)
  }
  Write-Verbose "Testing $computername"
  $data = Get-WmiObject @wmiParams
  Write-Verbose $data.OSArchitecture
  $data.OsArchitecture -match "64"

} #try

Catch {
  Switch -wildcard ($error[0].Exception.Message) {
   "The RPC Server*" { 
      Write-Warning "Can't connect to server. Verify name and availability." 
      }
   "Access is denied*" {
      Write-Warning "Access denied. Check your permissions."
      }
   "Invalid query*" {
      Write-Warning "WMI information not available. OS version may Windows 2003 or earlier."
      }
  Default { 
    Write-Error $error[0]
    }
  } #switch
} #catch

Finally {
    Write-Verbose "Finished testing"
} #finally

} #close function

The function takes a computername and optionally a PSCredential. You can use a saved PSCredential object or specify the name and you will be prompted.

Test-Is64Bit-01
The other design element is that if there is an exception caught I have a Switch statement to checks the message and writes a custom warning.
Test-Is64Bit-02

There's no reason you can't use the one-liner. But if you want to add a bit more robustness and create a re-usable tool, it doesn't take much to turn it into a simple function.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “Test 64-Bit Operating System”

  1. Denham Dagger says:
    May 16, 2013 at 2:55 pm

    Even easier.

    [System.IntPtr]::Size

    If the result is 4 then x86, 8 then x64

    1. Jeffery Hicks says:
      May 16, 2013 at 3:00 pm

      Easier is a relative term. Certainly it is less typing. But it assumes some knowledge of .NET and it only works locally. Doesn’t help me test a remote system.

  2. Pingback: My Daily Shared Bookmarksfboiton's blog | fboiton's blog
  3. Pasquale Lantella says:
    June 10, 2013 at 9:04 am

    we use the win32_computersystem class to identify the OS Architecture like the function below

    Function Get-OSArchitecture {

    param (
    [string]$ComputerName
    )

    $OSarc = (Get-WMIObject win32_computersystem -ComputerName $ComputerName).SystemType
    If ( $OSarc -match ’86’) { return “32-Bit”}
    ElseIF ( $OSarc -match ’64’ ){ return “64-Bit” }
    Else { return $Null }

    } # end Function Get-OSArchitecture

    1. Jeffery Hicks says:
      June 10, 2013 at 11:18 am

      Here’s a variation on your technique in a one line command:

      PS Scripts:\> ([regex]”[x|X]\d{2}”).Match( (get-wmiobject win32_computersystem -comp serenity).SystemType).value
      x64
      PS Scripts:\> ([regex]”[x|X]\d{2}”).Match( (get-wmiobject win32_computersystem -comp novo8).SystemType).value
      X86

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d