Solved

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

Posted on 2014-03-20
12
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 40

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 40

Assisted Solution

by:footech
footech earned 500 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 40

Accepted Solution

by:
footech earned 500 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
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 tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

729 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