Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Finds the Windows version including Edition, Version and OS Build numbers for local or remote computers
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ValueFromPipeline=$true
)]
[string[]]$ComputerName = $env:COMPUTERNAME
)
Begin
{
$Table = New-Object System.Data.DataTable
$Table.Columns.AddRange(@("ComputerName","Windows Edition","Version","OS Build"))
}
Process
{
Foreach ($Computer in $ComputerName)
{
$Code = {
$ProductName = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name ProductName).ProductName
Try
{
$Version = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name ReleaseID -ErrorAction Stop).ReleaseID
}
Catch
{
$Version = "N/A"
}
$CurrentBuild = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name CurrentBuild).CurrentBuild
$UBR = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name UBR).UBR
$OSVersion = $CurrentBuild + "." + $UBR
$TempTable = New-Object System.Data.DataTable
$TempTable.Columns.AddRange(@("ComputerName","Windows Edition","Version","OS Build"))
[void]$TempTable.Rows.Add($env:COMPUTERNAME,$ProductName,$Version,$OSVersion)
Return $TempTable
}
If ($Computer -eq $env:COMPUTERNAME)
{
$Result = Invoke-Command -ScriptBlock $Code
[void]$Table.Rows.Add($Result.Computername,$Result.'Windows Edition',$Result.Version,$Result.'OS Build')
}
Else
{
Try
{
$Result = Invoke-Command -ComputerName $Computer -ScriptBlock $Code -ErrorAction Stop
[void]$Table.Rows.Add($Result.Computername,$Result.'Windows Edition',$Result.Version,$Result.'OS Build')
}
Catch
{
$_
}
}
}
}
End
{
Return $Table
}
@Mata101Alex
Copy link

Mata101Alex commented Apr 26, 2019

What do you have to change to query remote machines?

@lveitch
Copy link

lveitch commented Jun 12, 2019

What do you have to change to query remote machines?

I was curious about this as well. I have a txt file with all my machines in it and I would love to run this against

@SMSAgentSoftware
Copy link
Author

SMSAgentSoftware commented Jun 12, 2019

Just pass a computer name or list of computer names to the ComputerName parameter.

@desquinn
Copy link

desquinn commented Jan 4, 2020

Suggest changing catch to the following so it deals with failed wirm connections with tidier output. Thanks for the script.

` Catch

        {

            Write-host "Failed connection to $computer" -ForegroundColor Yellow
            Write-Verbose $_ 
        }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment