Detecting 64-bit Operating System

by Jan 7, 2015

Windows 7/Server 2008 R2

To detect whether a script runs in a 32-bit or 64-bit environment is fairly easy: check a pointer size, and test whether it is 4 or 8 bytes:

if ([IntPtr]::Size -eq 8)
{
    '64-bit'
}
else
{
    '32-bit'
} 

This will not tell you anything about the type of operating system, though, since your PowerShell script could run as a 32-bit process on a 64-bit machine.

To check the OS, try this:

if ([Environment]::Is64BitOperatingSystem)
{
    '64-bit'
}
else
{
    '32-bit'
} 

While at it, the Environment class can check the architecture of your process as well:

if ([Environment]::Is64BitProcess)
{
    '64-bit'
}
else
{
    '32-bit'
} 

Twitter This Tip! ReTweet this Tip!