Solved

Search domain for accounts that end with a particular letter

Posted on 2014-03-13
12
353 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

825 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