list all users who are carted from past 1 year. and check if they exists in a few DL's

poramboku
poramboku used Ask the Experts™
on
This is not giving any results.
can you help find fix this..

I am trying to list all users who are carted from past 1 year. and check if they exists in a few DL's

Get-ADUser -Filter * -SearchBase "MY OU path" -SearchScope Subtree -Properties * | ?{$_.Created -ge ((Get-Date).AddDays(-365)).Date -and ($_.MemberOf -contains "DL1" -or $_.MemberOf -contains "DL2")} | select -Property name, LastLogonDate, PasswordLastSet,UserPrincipalName,EmailAddress

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try to debug one part at time.

Start with:

Get-ADUser -Filter * -SearchBase "MY OU path" -SearchScope Subtree -Properties *

Do you get anything? if not, check you conditon

If you get something, then add the next condition

Get-ADUser -Filter * -SearchBase "MY OU path" -SearchScope Subtree -Properties * | ?{$_.Created -ge ((Get-Date).AddDays(-365)).Date

and so on...
Lead SaaS Infrastructure Engineer
Commented:
This should do the needful.

On my phone so can't test for syntax errors:

#####

$DebugPreference = "Continue"

$AYearAgo = ((Get-Date).AddDays(-365)).Date

#Debug show the date a year ago
Write-Output
$AYearAgo | Write-Debug

$UsersAndGroups = Get-ADUser -Filter {whenCreated -ge $AYearAgo} -Properties whenCreated, MemberOf

#Debug show shortlist of users and groups
Write-Output
$UsersAndGroups | write-debug

#Output long list of users to groups:
Foreach ($User in $UsersAndGroups) {
Write-Output
Foreach ($Group in $($User| Select -ExpandProperty memberof) ) {
Write-Output "$User ---> $Group"
}

}

#####

Open in new window

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Ahh, I see you had a list of groups.  Okay we can modify to accomodate that and pull the matches only.  Almost finished with commute though, so I'll have to do that later, or somone else can expand upon this.
Douglas SuyemotoGlobal Support Services Engineer

Commented:
Don't use
-contains
since you're trying a string match, instead use
-like "*DL1*"

Author

Commented:
direcited

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial