How-to: Get the Operating System and Service Pack level

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-Object 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-Object Caption, Version, BuildNumber
}

A function which accepts a ComputerName and returns the OS/version:

Get-OSVersion.ps1

function Get-OSVersion {
 <#
 .Synopsis
 Get the OS 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-Object 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-Object {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
 $osinfo | Select-Object Name, version, servicepackmajorversion,
 BuildNumber, CSName, OSArchitecture, OperatingSystemSKU
 $os = $osinfo.Name # OS Name
 $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"
 # "Full OS String: $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:

PS C:\> Get-OSVersion
PS C:\> Get-OSVersion 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

PowerShell versions and OS compatibility.
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

(追記) (追記ここまで)
Copyright © 1999-2026 SS64.com
Some rights reserved

AltStyle によって変換されたページ (->オリジナル) /