Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


GetADuser -filer sAMAccountName with 5 character

Posted on 2013-01-23
Medium Priority
Last Modified: 2013-01-23
Is it possible to include a filter for sAMAccountName with exactly 5 character in the below script. My goal is to select users with 5 character usernames that has an email.

Get-ADUser -Filter {mail -like '*@*'} -SearchBase......

Question by:pacch
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
  • 2
  • 2
LVL 40

Expert Comment

ID: 38809051
Try with..
Get-ADUser -Filter {mail -like '*@*'} | ? {$_.Samaccountname.length -eq 5}

Author Comment

ID: 38809407
Tanks for the reply - i get this error when trying

Where-Object : A parameter cannot be found that matches parameter name 'SearchB
At line:1 char:109
+ $users += Get-ADUser -Filter {mail -like '*@*'} | ? {$_.Samaccoun
tname.length -eq 5} -SearchBase <<<<  "OU=myOU,DC=
myDCl,employeeType,@{n='OU';e={$_.canonicalname -replace "/$($_.cn)",""}}
    + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBi
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm

LVL 40

Accepted Solution

Subsun earned 2000 total points
ID: 38809425
It should be like..
Get-ADUser -Filter {mail -like '*@*'} -SearchBase "OU=myOU",DC=myDC,DC=com" -Properties * | ? {$_.Samaccountname.length -eq 5} | Select-Object sAMAccountName,givenName,sn,mail,employeeType,company,Department,@{n='OU';e=@{n='OU';e={($_.canonicalname -Split "/")[-2]}} | Export-Csv "C:\file.csv" -Encoding "Unicode"

Open in new window


Author Closing Comment

ID: 38809505
Works Thanks!!

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 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