Solved

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

Posted on 2014-03-20
12
305 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
Comment Utility
try

~

symbol instead
0
 
LVL 2

Author Comment

by:MilesLogan
Comment Utility
where exactly ?
0
 
LVL 3

Expert Comment

by:Mutogi
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:allen_rich
Comment Utility
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
Comment Utility
Hi Footech .. the output file still had *t accounts that where more then 8 characters ..
0
 
LVL 2

Author Comment

by:MilesLogan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks footech .. both options worked ...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Resolve DNS query failed errors for Exchange
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now