This morning I needed to quickly get Windows 10 version information for all workstations in a domain, so I wrote the below PowerShell script. This basic script only needed a few things to get the job done; a way to get all workstations in the domain, a way to only check computers that are online at the time, so the script doesn’t take too long with all the failures generated by offline systems, and the registry keys to determine the current OS build and minor version.
Import-Module -Name ActiveDirectory
$all_computers = Get-ADComputer -Filter * -SearchBase 'OU=computers,DC=somedomain,DC=com' | Select-Object -Property Name
$ExportPath = "$env:TEMP\$(Get-date -Format 'yyyyMMddhhmmss')_workstation_os_build_report.csv"
foreach ($c in $all_computers.name)
{
if (Test-Connection -ComputerName $c -count 1 -Quiet ) {
Write-Host "Processing $c" -ForegroundColor Cyan
$CurrentBuild = ""
$UBR = ""
$OSVersion = ""
$ComputerSystem = ""
$props = ""
$obj = ""
$CurrentBuild = Invoke-Command -ComputerName $c -ScriptBlock { (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' –Name CurrentBuild).CurrentBuild } -ErrorAction SilentlyContinue
$UBR = Invoke-Command -ComputerName $c -ScriptBlock { (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' –Name UBR).UBR } -ErrorAction SilentlyContinue
$OSVersion = $CurrentBuild + "." + $UBR
$ComputerSystem = Get-WmiObject -ComputerName $c -Class Win32_ComputerSystem -ErrorAction SilentlyContinue
$props = [ordered]@{
'HostName' = $ComputerSystem.Name;
'OSVerion' = $OSVersion
}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj | Export-Csv -Path $ExportPath -NoTypeInformation -Append -NoClobber -Force
}
else {
Write-Host "$c is offline..." -ForegroundColor Green
}
}
Write-Host "Output csv file is located here: `n `n $ExportPath `n" -ForegroundColor Yellow
Obviously there are a few issues with this script. It won’t get systems that are turned off, PowerShell remoting needs to be enabled/working, and in a large domain, it’s probably going to take a long time without some sort of parallelisation or a more efficient way of querying each host…. regardless, this was just a quick indicator for me of the general patch levels of systems in the domain.
Code also on my github here.
Thanks for reading – Jesse