Solved

GetADuser -filer sAMAccountName with 5 character

Posted on 2013-01-23
4
1,182 Views
Last Modified: 2013-01-23
Hi,
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......

Regards
0
Comment
Question by:pacch
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

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

Author Comment

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

Where-Object : A parameter cannot be found that matches parameter name 'SearchB
ase'.
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
   ndingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
   ands.WhereObjectCommand

Regards
0
 
LVL 40

Accepted Solution

by:
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

0
 

Author Closing Comment

by:pacch
ID: 38809505
Works Thanks!!
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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