Jump to content

Power shell script for counting all enabled computers with their OS


Recommended Posts

Posted

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.

Posted (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 by Norphy
Posted (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 by Norphy
Posted
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?
Posted

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

  • Thanks 1
Posted
Get-ADComputer -Filter {Enabled -eq $True} -Properties OperatingSystem, OperatingSystemVersion | group-Object -Property OperatingSystemVersion, OperatingSystem
Posted

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.

Posted (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 by HPlum78
Posted

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

  • Thanks 2
Posted

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

Posted (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 by k-strider
Posted
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!

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...