• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

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
0
MilesLogan
Asked:
MilesLogan
  • 7
  • 5
1 Solution
 
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
 
Justin YeungSenior Systems EngineerCommented:
no it will not it is only $_.enabled eq $true.
0
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

 
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
 
Justin YeungSenior 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
 
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

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now