Get your Operating System with PowerShell

Adam Bertram

Adam Bertram

Read more posts by this author.

Here’s a great way to get the operating system name, service pack and architecture of a local computer.

This tiny script uses WMI to first gather the necessary attributes about the operating system.

## Gather the Caption (OS Name), CSDVersion (Service Pack) and OSArchitecture (x86 or x64)
$OsAttribs = Get-WmiObject -Class Win32_OperatingSystem | select caption,csdversion,osarchitecture

## If CSDVersion (Service Pack) is null then I'm assuming this means no service pack at all so it's actually RTM (Release to Manufacturing)
$ServicePack = @{$true = $OsAttribs.CSDVersion; $false = 'RTM'}[$OsAttribs.CsdVersion -ne $null]

## Use the [pscustomobject] type accelerator to create a custom object from a hash table with all the attributes
## Always output objects if you can from your scripts

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!