Search domain for accounts that end with a particular letter

Hi EE ..

This is weird one .. I would like to have a PS script that will search for all accounts that end with the letter T , need to search the entire domain , except for the Disabled Accounts OU .

The output file to give me the SamAccountName,Name, title ,Company
LVL 2
MilesLoganAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Justin YeungConnect With a Mentor Senior Systems EngineerCommented:
get-aduser -filter {distinguishedName -notlike "*,OU=disabled OU,DC=domain,DC=domain,DC=domain} -properties * | ? {$_.samaccountname -like "*t" -and $_.enabled -eq $true} | select-object SamAccountName,Name, title ,Company | export-csv "path.csv"

this will exclude the users in the disabled OU and it has $_.enabled -eq $true which will only return account that is enabled. any disabled accounts will not be included in the result.
0
 
Justin YeungSenior Systems EngineerCommented:
get-aduser -filter * -properties * | ? {$_.samaccountname -like "*t" -and $_.enabled -eq $true} | select-object SamAccountName,Name, title ,Company | export-csv "path.csv"
0
 
MilesLoganAuthor Commented:
Hi Justing .. but this will search in the Disabled Accounts OU also .. I need it to not search that OU ..
0
Easily Design & Build Your Next Website

Squarespaceā€™s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
Justin YeungSenior Systems EngineerCommented:
no it will not it is only $_.enabled eq $true.
0
 
Justin YeungSenior Systems EngineerCommented:
if you want to filter a OU add the filter on it

get-aduser -filter {distinguishedName -ne "CN=Users,DC=domain,DC=domain,DC=domain} -properties * | ? {$_.samaccountname -like "*t" -and $_.enabled -eq $true} | select-object SamAccountName,Name, title ,Company | export-csv "path.csv"
0
 
MilesLoganAuthor Commented:
well .. see thats part of my issue also .. Accounts outside of the Disabled Accounts OU will be both enabled and disabled .. so I need the query to not look at the enabled status .. I just need all "t" accounts outside of the Disabled Accounts OU
0
 
MilesLoganAuthor Commented:
Thanks, I will give this a try in a few mins ..

CanI remove "-and $_.enabled -eq $true" from the search so it does include disabled accounts ?
0
 
Justin YeungSenior Systems EngineerCommented:
yes it will include the disabled at that point
0
 
Justin YeungSenior Systems EngineerCommented:
I changed it a bit

get-aduser -filter * -properties * | ? {$_.samaccountname -like "*t" -and $_.enabled -eq $true -and distinguishedName -notlike "*,OU=disabled OU,DC=domain,DC=domain,DC=domain} | select-object SamAccountName,Name, title ,Company | export-csv "path.csv"

for some reason the filter doesn't do the job.
0
 
MilesLoganAuthor Commented:
HI Justin ..
I left the search as you listed and I just modified the OU path and file name and received the error below .


The string is missing the terminator: ".
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
0
 
Justin YeungSenior Systems EngineerCommented:
you are missing the "

"ou=path",dc=so,dc=so,dc=so"
0
 
MilesLoganAuthor Commented:
Thanks so much man !!! wow
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.