-2

I am lost I need to write a query to give me all the groups that a user is the owner of in a CSV format. An alternative would be a list of all groups with their username and e-mail.

The CSV format should either be:

"GroupName", ,"givenname","surname","office","mail",

or (preferred)

"givenname","surname","office","mail","group1","group2","group3",etc...

I tried the following but it didn't work.

$test = 'OU=Practice Office,DC=us,DC=LMCorp,DC=com'
$test | ForEach {
    Get-ADGroup -Filter * -Properties ManagedBy -SearchBase $_
} | Select Name, ManagedBy,objectGUID,memberOf,givenname,surname,office,mail,‌​title,telephoneNumbe‌​r,department | Sort -Property Name | Out-File C:\temp\1.csv
Seth
  • 1,215
  • 15
  • 35
China Syndrome
  • 953
  • 12
  • 24
  • I have tried this with no luck $test = 'OU=Practice Office,DC=us,DC=LMCorp,DC=com' $test | ForEach {Get-ADGroup -Filter * -Properties ManagedBy -SearchBase $_ } | Select Name, ManagedBy,objectGUID,memberOf,givenname,surname,office,mail,title,telephoneNumber,department | Sort -Property Name | Out-File C:\temp\1.csv – China Syndrome Jan 16 '17 at 23:56
  • Please [edit] your question. Code in comments is unreadable. – Ansgar Wiechers Jan 17 '17 at 09:17
  • Is that LDAP string for the OU actually correct? You could skip the `ForEach` as you're only using `$test` anyway. That actually should get you a CSV as in your first format if you replace `Out-File` by `Export-Csv`. – Seth Jan 17 '17 at 14:03

1 Answers1

1

I don't see why your using a foreach. Wouldn't this work?

$test = ou location
$groups = get-adgroup -searchbase $test -properties ManagedBy

Then $groups will contain what you're looking for. Check its contents by just entering it at the PowerShell prompt:

$groups

Or

$groups[0]

Will give you the first element in the set. Then you can sort and pipe out to a file. Can you get that far, that you're getting the right AD objects returned?

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
datarocker
  • 31
  • 1
  • 6
  • not quite understanding, I am super new can you post a code sample for me to see? – China Syndrome Jan 17 '17 at 13:00
  • 1
    But he gave you the full example. The lines starting with `$test` (obviously you would have to add the LDAP string yourself and `$groups` can be used as is. Assuming you have the RSAT tools (for the AD PowerShell module) installed. You might have to do a `Import-Module ActiveDirectory` beforehand. – Seth Jan 17 '17 at 13:52