How to find all unlicensed Windows Servers using PowerShell

This is a quick post to show an example of a script that will search your domain for unlicensed Windows Server machines.

$servers =  get-content "C:\temp\servers.txt"
$ts = get-date -Format dd-MM-yyyy_hh-mm
$outputfile = "C:\temp\serverslicensestatus-" + $ts + ".csv"
$report = @()

foreach($server in $servers)
{
    $server
    $licdetail = Get-CimInstance SoftwareLicensingProduct -ComputerName $server -OperationTimeoutSec 30 -Filter "Name like 'Windows%'" | where { $_.PartialProductKey } | select Description, LicenseStatus, ProductKeyChannel
    $props=@{
            ServerName=$server
            LicenseStatus=$licdetail.LicenseStatus
            ProductKeyChannel=$licdetail.ProductKeyChannel 
            Description=$licdetail.Description
    }
    $Obj = New-Object PSObject -Property $props
    $report += $Obj
    $obj = $Null
    $licdetail = $null
}

$report | Export-Csv $outputfile -NoTypeInformation

Leave a Reply

Your email address will not be published. Required fields are marked *