0

I have a powershell command:

Get-ADGroupMember -identity "CCVGHCFinReadOnly" -Recursive | foreach{ get-aduser -properties employeeid $_} | select SamAccountName,objectclass,name,employeeid | Export-Csv "C:\Temp\AD Membership\GovReadOnly.csv

This script works great to return members of AD groups. I also want to add the count for auditing purposes. So the script should list all ad users and then a count at the bottom.

Right now if I use | Measure it gives me a count but that is all I get. So I can either get the actual users in the AD group OR the count. How can I get both with this one command?

2 Answers 2

1
$members = Get-ADGroupMember ... | select SamAccountName,objectclass,name,employeeid

$members | Export-Csv "C:\Temp\AD Membership\GovReadOnly.csv"
@($members).Count | Add-Content "C:\Temp\AD Membership\GovReadOnly.csv"
Sign up to request clarification or add additional context in comments.

1 Comment

Never thought of pumping the output to a variable then reading the var into the file and then counting the members. This is simple and brilliant. Thanks.
1

Off the top of my head side effects come to mind. In this example I am incrementing a variable outside of the pipeline from within the pipeline. I admit it feels kind of gross. Maybe somebody else can suggest a solution that does not rely on this sort of side effect.

$count = 0
Get-ADGroupMember -identity "CCVGHCFinReadOnly" -Recursive `
| foreach { 
    $count++
    get-aduser -properties employeeid $_} `
| select SamAccountName,objectclass,name,employeeid `
| Export-Csv "C:\Temp\AD Membership\GovReadOnly.csv

Now you can do whatever you want with $count.

1 Comment

This works but I agree it just doesn't feel good. I was doing something similar before I jumped over. Thanks though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.