Last active
August 16, 2024 20:46
-
-
Save SMSAgentSoftware/78659181ccbe0f59677209f3487d7030 to your computer and use it in GitHub Desktop.
Finds the Windows version including Edition, Version and OS Build numbers for local or remote computers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 | |
} |
Just pass a computer name or list of computer names to the ComputerName parameter.
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 $_
}`
Update to get Windows Build Codenames
[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","Codename"))
}
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
switch ($CurrentBuild)
{
10240 { $Codename = "1507" }
10586 { $Codename = "1511" }
14393 { $Codename = "1607" }
15063 { $Codename = "1703" }
16299 { $Codename = "1709" }
17134 { $Codename = "1803" }
17763 { $Codename = "1809" }
18362 { $Codename = "1903" }
18363 { $Codename = "19H2" }
19041 { $Codename = "20H2" }
19042 { $Codename = "20H2" }
19043 { $Codename = "21H2" }
19044 { $Codename = "22H2" }
}
$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","CodeName"))
[void]$TempTable.Rows.Add($env:COMPUTERNAME,$ProductName,$Version,$OSVersion,$Codename)
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',$Result.'CodeName')
}
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',$Result.'CodeName')
}
Catch
{
$_
}
}
}
}
End
{
Return $Table
}
@ernewes those code names are only valid for Windows 10, or not?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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