Solved

Search domain for accounts that end with a particular letter

Posted on 2014-03-13
12
355 Views
Last Modified: 2014-03-13
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
0
Comment
Question by:MilesLogan
  • 7
  • 5
12 Comments
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39927112
get-aduser -filter * -properties * | ? {$_.samaccountname -like "*t" -and $_.enabled -eq $true} | select-object SamAccountName,Name, title ,Company | export-csv "path.csv"
0
 
LVL 2

Author Comment

by:MilesLogan
ID: 39927127
Hi Justing .. but this will search in the Disabled Accounts OU also .. I need it to not search that OU ..
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39927144
no it will not it is only $_.enabled eq $true.
0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39927157
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
 
LVL 2

Author Comment

by:MilesLogan
ID: 39927159
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
 
LVL 14

Accepted Solution

by:
Justin Yeung earned 500 total points
ID: 39927169
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
 
LVL 2

Author Comment

by:MilesLogan
ID: 39927188
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39927204
yes it will include the disabled at that point
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39927238
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
 
LVL 2

Author Comment

by:MilesLogan
ID: 39927277
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
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39927296
you are missing the "

"ou=path",dc=so,dc=so,dc=so"
0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 39927957
Thanks so much man !!! wow
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question