?
Solved

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

Posted on 2014-03-20
12
Medium Priority
?
332 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
Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

 
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 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 40

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
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 …
Suggested Courses

764 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