Solved

GetADuser -filer sAMAccountName with 5 character

Posted on 2013-01-23
4
1,111 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
Comment Utility
Try with..
Get-ADUser -Filter {mail -like '*@*'} | ? {$_.Samaccountname.length -eq 5}
0
 

Author Comment

by:pacch
Comment Utility
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
Comment Utility
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
Comment Utility
Works Thanks!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

12 Experts available now in Live!

Get 1:1 Help Now