Using WMI/CIM to find the OS version on a remote machine is a one line command:
PS C:\> Get-CimInstance Win32_OperatingSystem -computer 'computer64' | select Name, version, servicepackmajorversion, BuildNumber, CSName, OSArchitecture, OperatingSystemSKU
This can be written as a simple one line function, or see below for a fuller example:
Function get-ver{
(Get-CimInstance Win32_OperatingSystem) | select Caption, Version, BuildNumber
}
A function which accepts a ComputerName and returns the OS/version:
Get-OSVersion.ps1
function Get-OSVersion { <# .Synopsis Get the operating system Version .Description The function gets the version of the operating system on a local or remote computer. .Parameter ComputerName Enter the name of a local or remote computer. The default is the local computer ("localhost"). .Parameter LogFile Enter the name of a file .Notes Get-OSVersion reads the OS properties from the Win32_OperatingSystem WMI/CIM class. .LINK https://ss64.com/ps/syntax-osversion.html .Outputs Displays several strings with different system information. The ComputerName and Service pack can optionally be output to a log file. .Example Get-OSVersion .Example get-osVersion Server64 .Example # Get all computers with a name ending with db $comps = get-adcomputer -filter {name -like "*db"} | select name # Then pipe the list of computer names to get-osversion $comps.name | get-osversion .Example # Read a list of computernames from a file and write all the OS versions to a log file. $computers = get-content c:\demo\computerlist.txt $computers | foreach {get-osversion $_ -logfile List_of_versions.txt} #> [cmdletbinding()] Param ( [parameter(ValueFromPipeline=$True)] $ComputerName="localhost", $Logfile ) PROCESS { write-host "[$ComputerName]" if( (Test-Connection $ComputerName -Quiet -count 1)) { $osinfo = Get-CimInstance Win32_OperatingSystem -computer $ComputerName | select Name, version, servicepackmajorversion, BuildNumber, CSName, OSArchitecture, OperatingSystemSKU $os = $osinfo.Name # OS Name $servicepack = $osinfo.servicepackmajorversion # Service pack level $bitness = $osinfo.OSArchitecture # 32 or 64 bit $build = $osinfo.BuildNumber # Build Number $machineName = $osinfo.CSName # Name of the machine $edition = $osinfo.OperatingSystemSKU # Windows Edition (home, enterprise etc) "Computer: $ComputerName Build: $build Bitness: $bitness Edition: $edition" # service pack may be useful for W7 # " ServicePack: $servicepack OS: $os" if ($Logfile) {"$ComputerName : $build : $bitness : $edition" | out-file -append -filepath $Logfile} } else { Write-Error "$ComputerName Not Responding" } } # End of PROCESS block / next ComputerName }
The function can be added to an existing script or psprofile, or module or just pasted into the current session.
You can then run the function with:
GetOSVersion.ps1 computer64
“The hero has died as a modern man; but as eternal man - perfected, unspecific, universal man, - he has been reborn” ~ Joseph Campbell
Related PowerShell Cmdlets:
Get-CimInstance - Get WMI class information.
OS Version - How to retrieve the OS version in PowerShell (several methods compared).
psp - PowerShell Ping - test if a machine is online.
VER - Display version information.
WQL (WMI Query Language) - msdn.microsoft.com