I'm trying to create a PowerShell script to list all groups in the active directory that are created in the last 21 days. This works fine but sometimes a group can be managed by a user or by another security group. And I would like it to display the 'Display name' of the user/security group.
What I have created so far:
$When = ((Get-Date).AddDays(-21)).Date
Get-ADGroup -SearchBase "OU=Groups,OU=BEL,OU=EU,DC=domain,DC=net" -Filter {whenCreated -ge $When} -Properties * |
Select whenCreated, cn, displayName,GroupScope, GroupCategory, description, info,@{Label='managedBy';Expression={
if(Get-ADObject $_.managedBy -Filter 'ObjectClass -eq "user"')
{(Get-ADUser $_.Manager -Properties displayName).displayName}
Else{(Get-ADGroup $_.managedBy -Properties cn).cn}
}
| Export-Csv "New groups -21 days.csv" -NoTypeInformation -Delimiter ";" -Encoding utf8; start "New groups -21 days.csv"
It says: Missing '=' operator after key in hash literal and I can't seem to figure out what needs to be changed. Thank you for your help.
Best Answer
You are missing underscores from your variables $.managedBy and $.Manager. Both should be
$_.managedBy
$_.Manager
and a closing brace is missing.