Sebastian P
Sebastian P

Reputation: 1

PS to get info from AD: group_name, employeeid, name, samacoountname

I am trying to get some info from AD. I need a csv with 4 columns: group_name, emplyeeid, name and sammacount name for several security groups. I have the first one that gets me the group member and the name: Get-ADGroup -Filter {Name -like "security_group*"} | foreach {$group =

$_.Name;  Get-ADGroupMember $group | select @{N='group'; E={$group}},Name} | select group,Name| Export-CSV -Path C:\temp\test.csv 

And another one that gets me the employeeid, name, samacoountname:

Get-ADGroup -filter {Name -like "group_name*"} |
foreach { 
 Write-Host "Exporting $($_.name)"
 $name = $_.name -replace " ","-"
 $file = Join-Path -path "C:\temp\test" -ChildPath "$name.csv"
 Get-ADGroupMember -Identity $_.distinguishedname -Recursive |  
 Get-ADObject -Properties SamAccountname,employeeid |
 Select employeeid,Name,SamAccountName |
 Export-Csv -Path $file -NoTypeInformation
}

This gets me a different csv file for every group. That's ok, because I can copy all to the same csv. I have tried to put all this into one script to give me all 4 colums, but I can't seem to get it right. Any help would be appreciated!

Upvotes: 0

Views: 361

Answers (1)

Sebastian P
Sebastian P

Reputation: 1

I figured it out:

Get-ADGroup -filter {Name -like "group_name*"} |
foreach { 
 Write-Host "Exporting $($_.name)"
 $name = $_.name -replace " ","-"
 $file = Join-Path -path "C:\temp\test" -ChildPath "$name.csv"
 Get-ADGroupMember -Identity $_.distinguishedname -Recursive |  
 Get-ADObject -Properties SamAccountname,employeeid |
 Select @{N='group'; E={$name}},employeeid,Name,SamAccountName |
 Export-Csv -Path $file -NoTypeInformation
}

Upvotes: 0

Related Questions