Grouping by department and counting users - Powershell

I'm trying to output all users with a certain department and count them also using PowerShell.

The output that I'm trying to get is

Department1
User1
User2
User3

3 Users

Department2
User1
User2

2 users


Here's what I am using but all I get a single list of users:

Department1
User1
User2
User3
User1
User2


$businesslines = "Technology Support Svcs", "Development"


ForEach ($line in $businesslines)



{

get-aduser -filter {department -like $line -and enabled -eq $true} | Select-Object name 

}

Open in new window

LVL 7
Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Something like this?
$businessLines = "Technology Support Svcs", "Development"
$businessLines | ForEach-Object {
	Get-ADUser -Filter "(enabled -eq '$true') -and (department -like '$($_)')" -Property Department, DisplayName |
		Group-Object -Property Department |
		ForEach-Object {
			"Department '$($_.Name)' [$($_.Count) users]"
			$_.Group | ForEach-Object {
				"    $($_.SamAccountName) ($($_.DisplayName))"
			}
			''
		}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)Author Commented:
Yes. Perfect.


Is it possible to export this to a CSV?
0
Tim CurwickAutomation ConsultantCommented:
It can be done a bit more efficiently with a single AD call. The AD filter does not support the -in operator, but we can dynamically build an equivalent. We're not worried about length, as the maximum filter size is 4 MB. Group-Object then groups the results for us, and we output each group object however we want.

$Depts = @(
    'Technology Support Svcs'
    'Development' )

$ADQuery = ( $Depts | ForEach-Object { "Department -eq '$_'" } ) -join ' -or '

Get-ADUser -Filter $ADQuery -Properties 'Department' |
    Group-Object -Property Department |
    ForEach-Object {
        $_.Name
        $_.Group | Select-Object -ExpandProperty Name
        ''
        [string]$_.Group.Count + ' users'
        '' } 

Open in new window

0
Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)Author Commented:
Let me ask you this. Can I call every department that ANY user has? And then group them including a group for $null?
0
oBdACommented:
It wouldn't be PowerShell if you couldn't ...
Get-ADUser -Filter "enabled -eq '$true'" -ResultSetSize $null -Property Department, DisplayName |
	Group-Object -Property Department |
	Sort-Object -Property Name |
	ForEach-Object {
		If ($_.Name) {
			"Department '$($_.Name)' [$($_.Count) users]"
		} Else {
			"<No department> [$($_.Count) users]"
		}
		$_.Group | ForEach-Object {
			"    $($_.SamAccountName) ($($_.DisplayName))"
		}
		''
	}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.