Fun with Powershell - Get the "managedby" property for a list of groups

If you ever wanted to get the managedby property for a bunch of groups in active directory, here is a small script which helps you achieve that.

Import-Module activedirectory
$groups = Get-ADGroup -filter * -SearchBase "OU=Distribution Groups,OU=Groups,OU=Common,OU=IN,OU=AC,DC=contoso,DC=com" | select samaccountname
$groups | Export-Csv -Path "C:\powershell\Scripts\DL Managers\groups.csv"
$content = Get-Content -Path "C:\powershell\Scripts\DL Managers\groups.txt"
$valid = @()

foreach ($group in $content)

$grp = Get-ADGroup -Identity $group -Properties * | select Name,samaccountname,ManagedBy | sort ManagedBy
$valid+= $grp

$valid | export-csv -Path 'C:\powershell\Scripts\DL Managers\managers.csv' -NoTypeInformation -UseCulture