Good afternoon, all.
I am trying to perform a search in P/S for SamAccountName that contains / starts with "svc_", and does not belong to a group called "disconnected", and write that to an Excel file.
What I am trying, at least for the syntax, doesn't result in anything. I know there are 300+ accounts that should show.
What am I declaring wrong?
get-aduser -filter * -properties *|? {$_.samaccountname -like "svc_" -and $_.MemberOf -eq "disconnected"}
I am also looking to do the same for those SamAccountName results that are not part of a group. I thought "-neq" would work (not equal), but I guess that value is wrong?
get-aduser -filter * -properties *|? {$_.samaccountname -like "svc_" -and $_.MemberOf -neq "disconnected"}
Once my mistakes are figured out, I will add | Export-Csv -Path $CSVfile -NoTypeInformation to have it write to a csv file.
Thank you in advance for all the assistance.
Don't filter with powershell when active-directory can do it for you, its many times more efficient that way:
As for the problem with your current code:
Should use a wildcard after
svc_:And:
Will never match since
MemberOfis a collection ofDistinguishedName.Notes:
The above code only looks for
userobjects, if you need to find members of mentioned group of anyobjectClass, then you can changeGet-ADUsertoGet-ADObject.This code only looks for direct members of the mentioned group, if you need to find the recursive members you can use a LDAP_MATCHING_RULE_IN_CHAIN. For this the filter would look like: