GetADuser -filer sAMAccountName with 5 character

Posted on 2013-01-23
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 "/$($",""}}
    + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBi
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm

LVL 40

Accepted Solution

Subsun earned 500 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

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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