Danny11717 Posted February 11, 2025 Posted February 11, 2025 Hi Everyone, I have got myself stuck on a bit of a problem. I need to run a script to count all of the computers in the AD that are enabled with their OS build. I have tried to search far and wide for the script but it is so specific I can only find the script for enabled computers on the AD and that is it. I just want to be careful and not to accidentally delete something.
Norphy Posted February 11, 2025 Posted February 11, 2025 (edited) So you're trying to find disabled computer accounts in AD? At the very basic level: Get-ADComputer -filter * | where-object {$_.enabled -eq $false} will get you a list of disabled accounts. You can put that into an object then loop through it to remove the accounts. Edited February 11, 2025 by Norphy
Danny11717 Posted February 11, 2025 Author Posted February 11, 2025 Hi @Norphy I am trying to find the amount of enabled computers on the AD with the OS versions.
Norphy Posted February 11, 2025 Posted February 11, 2025 (edited) OK, I misunderstood I don't know that you can get the exact build number from an AD object. AD does have the following properties: OperatingSystem : Windows 11 Enterprise OperatingSystemHotfix : OperatingSystemServicePack : OperatingSystemVersion : 10.0 (22631) Any of those useful to you? If you're trying to get all enabled Windows 10 22H2 builds for instance, you could do the following: Get-ADComputer -filter * -properties OperatingSystemVersion | where-object {$_.OperatingSystemVersion -match "19045" -and $_.enabled -eq $true} Edited February 11, 2025 by Norphy
Danny11717 Posted February 11, 2025 Author Posted February 11, 2025 That is perfectly fine. Does this give you an exact number of computers in that format telling you the operating system and operating system version or will this print out one by one?
Norphy Posted February 11, 2025 Posted February 11, 2025 That will print them all out one by one. If you want a count, there's two ways of doing it: $(Get-ADComputer -filter * -properties OperatingSystemVersion | where-object {$_.OperatingSystemVersion -match "19045" -and $_.enabled -eq $true}).count or $22H2 = Get-ADComputer -filter * -properties OperatingSystemVersion | where-object {$_.OperatingSystemVersion -match "19045" -and $_.enabled -eq $true} $22H2.count 1
HPlum78 Posted February 11, 2025 Posted February 11, 2025 Get-ADComputer -Filter {Enabled -eq $True} -Properties OperatingSystem | group-Object -Property OperatingSystem
HPlum78 Posted February 11, 2025 Posted February 11, 2025 Get-ADComputer -Filter {Enabled -eq $True} -Properties OperatingSystem, OperatingSystemVersion | group-Object -Property OperatingSystemVersion, OperatingSystem
HPlum78 Posted February 11, 2025 Posted February 11, 2025 oh and when it dont fit in the window pipe it in to FL so | FL on the end of the above...
psydii Posted February 11, 2025 Posted February 11, 2025 I think norphy's first example doesn't need the $ at the front. Generic solution to answer the question "how many 'things' in 'state' are there": (clever powershell cmdlets and filtering to get all the things in a state).count By wrapping some powerhshell cmdlets in parenthesis their output, In effect, becomes a temporary object whose methods and properties can be utilized. In this case the .count property.
HPlum78 Posted February 11, 2025 Posted February 11, 2025 (edited) $CmpGroups | where {$_.Name -contains "10.0 (20348), Windows Server 2022 Standard"} | select -ExpandProperty Group To expand on @psydii comment above, that would pull out the computers that are that version and OS name then pump the computer objects out or you can pipe it in to measure or export- or (enter cmdlet here) Given that you put that into a var called $CmpGroups Sorry I am spewing code and making assumptions so here is what should be the first bit: $CmpGroups = Get-ADComputer -Filter {Enabled -eq $True} -Properties OperatingSystem, OperatingSystemVersion | group-Object -Property OperatingSystemVersion, OperatingSystem Edited February 11, 2025 by HPlum78
jthompson Posted February 11, 2025 Posted February 11, 2025 Can't resist Get-ADComputer -Filter { Enabled -eq $True } -Properties OperatingSystem, OperatingSystemVersion | Group-Object -Property OperatingSystem, OperatingSystemVersion | Sort-Object Name | Format-Table -Property Name, Count That will give you output like: Name Count ---- ----- Windows 10 Education, 10.0 (19045) 123 Windows 11 Education, 10.0 (22631) 456 Windows 11 Education, 10.0 (26100) 789 Windows Server 2019 Datacenter, 10.0 (17763) 4 Windows Server 2019 Standard, 10.0 (17763) 5 Windows Server 2022 Standard, 10.0 (20348) 6 2
k-strider Posted February 12, 2025 Posted February 12, 2025 for my push to make sure everything moves win 11 (or win 10 LTSC) i run below PS then in excel sort by, OperatingSystem, then OperatingSystemVersion, then CanonicalName (this gives me them in OU in order) - i then Filter to remove Enabled false to remove objects that are disabled. at the mo im doing this daily as we have pushed some reg settings at various ous to force win 10 up to 11 where it qualifies Get-ADComputer -Filter * -Property * | Select-Object Name,Enabled,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion,DistinguishedName,CanonicalName,Modified | Export-CSV AllWindows.csv -NoTypeInformation -Encoding UTF8 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate] "ProductVersion"="Windows 11" "TargetReleaseVersionInfo"="24H2" "TargetReleaseVersion"=dword:00000001
k-strider Posted February 12, 2025 Posted February 12, 2025 (edited) Can't resist Get-ADComputer -Filter { Enabled -eq $True } -Properties OperatingSystem, OperatingSystemVersion | Group-Object -Property OperatingSystem, OperatingSystemVersion | Sort-Object Name | Format-Table -Property Name, Count That will give you output like: Name Count ---- ----- Windows 10 Education, 10.0 (19045) 123 Windows 11 Education, 10.0 (22631) 456 Windows 11 Education, 10.0 (26100) 789 Windows Server 2019 Datacenter, 10.0 (17763) 4 Windows Server 2019 Standard, 10.0 (17763) 5 Windows Server 2022 Standard, 10.0 (20348) 6 I clearly have some way to go :-/ PowerShell 7.4.6 [Constrained Language Mode] Get-ADComputer -Filter { Enabled -eq $True } -Properties OperatingSystem, OperatingSystemVersion | Group-Object -Property OperatingSystem, OperatingSystemVersion | Sort-Object Name | Format-Table -Property Name, Count Name Count ---- ----- 2 Windows 10 Education, 10.0 (19045) 207 Windows 10 Enterprise LTSC, 10.0 (19044) 65 Windows 11 Education, 10.0 (22000) 7 Windows 11 Education, 10.0 (22631) 12 Windows 11 Education, 10.0 (26100) 152 Windows Server 2016 Datacenter, 10.0 (14393) 2 Windows Server 2016 Standard, 10.0 (14393) 1 Windows Server 2019 Standard, 10.0 (17763) 4 Windows Server 2022 Standard, 10.0 (20348) 14 Edited February 12, 2025 by k-strider
jthompson Posted February 13, 2025 Posted February 13, 2025 I clearly have some way to go :-/ My counts were illustrative examples. My real output prompted me to remove some old computer objects that haven't been used for a few years!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now