How to find Windows OS version using PowerShell

For troubleshooting purposes, or before deploying any software, it is good to know what is the Windows Operating system version that is currently running. We can easily find the OS details from My Computer properties, but if you want to get details from your customer’s machine to troubleshoot any issue, PowerShell is the best option to get all the required machine details.

In PowerShell, we can find operating system details in different ways, but to be safe we can use the WMI-based cmdlet Get-WmiObject, this command is compatible with Windows PowerShell 2.0. Using this command we can query the WMI class Win32_OperatingSystem to get the OS version number:

(Get-WmiObject Win32_OperatingSystem).Version

The above command only returns the os version number. Run the following command to get the display name of your Windows version.

(Get-WmiObject Win32_OperatingSystem).Caption
Output :
Microsoft Windows 7 Ultimate

We can use the select command to get the output of all the required OS-related properties.

Get-WmiObject Win32_OperatingSystem |
Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber | FL

We can use the Get-WmiObject cmdlet in short form gwmi.

(gwmi win32_operatingsystem).caption

Get OS version of a remote computer

We can easily get the OS version details of a remote computer by adding the parameter -ComputerName to Get-WmiObject.

Get-WmiObject Win32_OperatingSystem -ComputerName "Remote_Machine_Name" |
Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber | FL

Connecting a remote server/machine may require providing admin user credentials. In this case, you may receive the error message “Get-WmiObject : Access is denied“. Use the below command to pass user credentials to the Get-WmiObject command.

$PSCredential = Get-Credential "ComputerName\UserName"
#$PSCredential = Get-Credential "DomainName\UserName"

Get-WmiObject Win32_OperatingSystem -ComputerName "Remote_Machine_Name" -Credential $PSCredential |
Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber | FL

Get OS details for a list of remote computers using PowerShell

You can use the following PowerShell script to find OS version details for multiple remote computers. First, create a text file named as “computers.txt” which includes one computer name in each line. You will get the output of the machine name, OS name, and version number in the CSV file OS_Details.csv.

Get-Content "C:\Temp\computers.txt"  | ForEach-Object{
$os_name=$null
$os_version=$null
$errorMsg=''
Try
{
$os_name = (Get-WmiObject Win32_OperatingSystem -ComputerName $_ ).Caption
}
catch
{
$errorMsg=$_.Exception.Message
}
if(!$os_name){
$os_name = "The machine is unavailable $errorMsg"
$os_version = "The machine is unavailable"
}
else{
$os_version = (Get-WmiObject Win32_OperatingSystem -ComputerName $_ ).Version 
}
New-Object -TypeName PSObject -Property @{
ComputerName = $_
OSName = $os_name
OSVersion = $os_version 
}} | Select ComputerName,OSName,OSVersion |
Export-Csv "C:\Temp\OS_Details.csv" -NoTypeInformation -Encoding UTF8
Advertisement

13 thoughts on “How to find Windows OS version using PowerShell”

  1. I am getting the below error when i run the 1st query, please help me

    Add-Member : Cannot bind argument to parameter ‘InputObject’ because it is null.
    At C:\Temp\OS_inventroy\OS.ps1:12 char:11
    + $result | add-member -MemberType NoteProperty -Name ComputerName -value $Compute …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Add-Member], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMember
    Command

    Add-Member : Cannot bind argument to parameter ‘InputObject’ because it is null.
    At C:\Temp\OS_inventroy\OS.ps1:13 char:11
    + $result | add-member -MemberType NoteProperty -Name BuildVersion -value $buildVe …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Add-Member], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMember
    Command

    Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null.
    At C:\Temp\OS_inventroy\OS.ps1:16 char:12
    + $results | export-csv “C:\Temp\OS_inventroy\server1.csv” -notypeinformation
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsv
    Command

    Reply
  2. i have the below error when i tried this command
    Export-Csv : Access to the path ‘C:\OS_Details.csv’ is denied.
    At line:25 char:1
    + Export-Csv “C:\OS_Details.csv” -NoTypeInformation -Encoding UTF8
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OpenError: (:) [Export-Csv], UnauthorizedAccessException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

    Reply
  3. I received the output and the PC is not down so what the issue

    ComputerName OSName OSVersion
    172.18.114.37 The machine is unavailable The machine is unavailable

    Reply
    • Can you run the following command after replacing the “Remote_Machine_Name” with your problematic machine name.?

      $PSCredential = Get-Credential

      Get-WmiObject Win32_OperatingSystem -ComputerName “Remote_Machine_Name” -Credential $PSCredential

      Reply

Leave a Comment