Solved

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

Posted on 2014-03-20
12
306 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
 
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 3

Expert Comment

by:Mutogi
ID: 39944371
0
 
LVL 39

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 39

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 39

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…
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 to another domain controller. Log onto the new domain controller with a user account t…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now