?
Solved

How to search for Active Directory accounts that are only a certain lenght

Posted on 2014-03-20
12
Medium Priority
?
343 Views
Last Modified: 2014-03-21
Hi EE ..

Can someone help me modify the search below so it only outputs SamAccountNames that are eight characters long ?

The account still needs to end with a "t" but only 8 characters .



Get-ADUser -Filter {SamAccountName -like "*t"} -properties * | ? { $_.whenCreated -ge (get-date "March 1, 2014")}|Select SamAccountName,description,whenCreated | Export-CSV Data.csv -NoTypeInformation
0
Comment
Question by:MilesLogan
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 3

Expert Comment

by:Mutogi
ID: 39944311
try

~

symbol instead
0
 
LVL 2

Author Comment

by:MilesLogan
ID: 39944328
where exactly ?
0
 
LVL 3

Expert Comment

by:Mutogi
ID: 39944333
Get-ADUser -Filter {SamAccountName -like "~t"} -properties * | ? { $_.whenCreated -ge (get-date "March 1, 2014")}|Select SamAccountName,description,whenCreated | Export-CSV Data.csv -NoTypeInformation
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:MilesLogan
ID: 39944356
Hi Mutogi .. how would "~t"} only pull accounts that are 8 characters only ?

so the for example ..

Pull these
1234567t
2222222t

NOT these
123456789t
999999999t
0
 
LVL 41

Expert Comment

by:footech
ID: 39944627
Try the following.
Get-ADUser -Filter {SamAccountName -like "*t"} -properties * | ? { $_.whenCreated -ge (get-date "March 1, 2014") -and ($_.samaccountname -match ".{8}")}|Select SamAccountName,description,whenCreated | Export-CSV Data.csv -NoTypeInformation

Open in new window

0
 
LVL 2

Expert Comment

by:allen_rich
ID: 39944877
Try the below command:

Get-ADUser -filter 'samaccountname -like "$t*"' -properties * | ? { $_.whenCreated -ge (get-date "March 1, 2014") -and ($_.samaccountname -match ".{8}")}|Select SamAccountName,description,whenCreated | Export-CSV Data.csv -NoTypeInformation

or

Get-ADUser -filter 'samaccountname -like "*"' -SearchBase "ou=faculty,ou=employees,ou=users2 ,dc=xxxxxx,dc=local" -Properties whencreated | Where-Object { $_.whencreated -gt (get-date).adddays(-180) } | select samaccountname -First 10
0
 
LVL 2

Author Comment

by:MilesLogan
ID: 39945087
Hi Footech .. the output file still had *t accounts that where more then 8 characters ..
0
 
LVL 2

Author Comment

by:MilesLogan
ID: 39945088
Allen_rich .. I tried your first one and it did not output anything .. I did not try the second since these are not in a single OU
0
 
LVL 41

Assisted Solution

by:footech
footech earned 2000 total points
ID: 39945661
Slight adjustment.
Get-ADUser -Filter {SamAccountName -like "*t"} -properties * | ? { $_.whenCreated -ge (get-date "March 1, 2014") -and ($_.samaccountname -match "^.{8}$")}|Select SamAccountName,description,whenCreated | Export-CSV Data.csv -NoTypeInformation

Open in new window

0
 
LVL 41

Accepted Solution

by:
footech earned 2000 total points
ID: 39945681
And another way that will also work.
Get-ADUser -Filter {SamAccountName -like "*t"} -properties * | ? { $_.whenCreated -ge (get-date "March 1, 2014") -and ($_.samaccountname.length -eq 8) } | Select SamAccountName,description,whenCreated | Export-CSV Data.csv -NoTypeInformation

Open in new window

0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 39945876
Thanks footech .. both options worked ...
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

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

Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

850 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