Search code examples
powershelloperating-systemfindwindows-server

Powershell to find Server Operating system


I had a task to find Operating System of all the servers we had in AD for some Microsoft Licenses requirement.

Has anyone done this?


Solution

  • I figured it out.

    Please feel free to use it and modify it. If you have questions, let me know.

    It's a simple command. Hopefully, it helps someone. This gives you the type of operating systems you have. I am filtering based on Windows Server only and computer accounts that are active. Then sort by name and select unique OS.

    Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem | Sort Name | select -Unique OperatingSystem
    

    Output:

    OperatingSystem
    ---------------
    Windows Server 2012 R2 Standard
    Windows Server 2008 R2 Standard
    Windows Server 2012 R2 Standard Evaluation
    Windows Server 2008 R2 Enterprise
    

    Next command is to get all the servers and show their Operating System. Again, I am filtering based on Windows server OS and Active computer accounts. I am sorting my list by Operating system:

    Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem | sort OperatingSystem | ft DNSHostName, OperatingSystem
    

    You can also save the above in a variable and then get the count of Servers in each operating system category:

    $Servers = Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem | Sort Name
    
    $servers | group operatingsystem