Missing '=' operator after key in hash literal

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.

2 Answers

You are missing underscores from your variables $.managedBy and $.Manager. Both should be $_.managedBy $_.Manager and a closing brace is missing.

$When = ((Get-Date).AddDays(-21))
Get-ADGroup -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"
2

Check your syntax on that calculated property. Your braces are out of balance (there should be another } at the end to close the hash expression).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like