Powershell to find Server Operating system

45,384

Solution 1

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  

Solution 2

$OSIs64BitArch = ([System.Environment]::Is64BitOperatingSystem)
$OSArchString = if ( $OSIs64BitArch ) {"x64"} else {"x86"}
$OSIsServerVersion = if ([Int]3 -eq [Int](Get-WmiObject -Class Win32_OperatingSystem).ProductType) {$True} else {$False}
$OSVerObjectCurrent = [System.Environment]::OSVersion.Version
if ($OSVerObjectCurrent -ge (New-Object -TypeName System.Version -ArgumentList "6.1.0.0")) {
    if ($OSVerObjectCurrent -ge (New-Object -TypeName System.Version -ArgumentList "6.2.0.0")) {
        if ($OSVerObjectCurrent -ge (New-Object -TypeName System.Version -ArgumentList "6.3.0.0")) {
            if ($OSVerObjectCurrent -ge (New-Object -TypeName System.Version -ArgumentList "10.0.0.0")) {
                if ( $OSIsServerVersion ) {
                    Write-Output ('Windows Server 2016 ' + $OSArchString + " ... OR Above")
                } else {
                    Write-Output ('Windows 10 ' + $OSArchString + " ... OR Above")
                }
            } else {
                if ( $OSIsServerVersion ) {
                    Write-Output ('Windows Server 2012 R2 ' + $OSArchString)
                } else {
                    Write-Output ('Windows 8.1 ' + $OSArchString)
                }
            }
        } else {
            if ( $OSIsServerVersion ) {
                Write-Output ('Windows Server 2012 ' + $OSArchString)
            } else {
                Write-Output ('Windows 8 ' + $OSArchString)
            }
        }
    } else {
        if ( $OSIsServerVersion ) {
            Write-Output ('Windows Server 2008 R2 ' + $OSArchString)
        } else {
            Write-Output ('Windows 7 OR Windows 7-7601 SP1' + $OSArchString)
        }
    }
} else {
    Write-Output ('This version of Windows is not supported.')
}
Share:
45,384
LT-
Author by

LT-

Summary: I am an enthusiastic IT professional currently serving as Systems Administrator for Russel Metals. I have over 8 years of progressive experience and have supported over Windows/Linux servers and a wide variety of network devices. I am a VMware, CCNA and ITIL certified professional and trained in AWS SysOps, Google Cloud CP100, and MCSA. I am also pursuing MCSE/MCSA certification, project management, and leadership training. I have supervised and led multi-functional teams of professionals to achieve performance targets successfully. I have managed larger projects efficiently and tackle complex challenges. Some of my accomplishments include: In my current position, I have implemented CyberArk (PAM solution) to handle privileged accounts and migrated Russel Metals from On-prem exchange to Office 365 using a hybrid approach. Completed a core Firewall transition from Cisco ASA5510 to FortiGate 90D, upgraded VMware from 4.0 to 5.5, and implemented Enterprise Cloud Services at LV Lomas Ltd. As IT specialist and Systems Administrator with IBM, I automated AD services, FTP services, and Symantec server-side client upgrade. I developed and deployed custom solutions and applications via SCCM, such as auto switching between LAN and Wi-Fi, Java, Microsoft Office, Project and Visio app installs.

Updated on February 21, 2020

Comments

  • LT-
    LT- over 4 years

    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?