Power Shell - Quest - How to pull all samAccountNames that are 7 or 8 characaters long

Posted on 2013-05-16
Last Modified: 2013-05-16

I am trying to search for all samAccountNames in my environment that are only 7 or 8 characters long .. any idea how I can do this with Quest Tools ? or any other tool ?
Question by:MilesLogan
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 70

Expert Comment

ID: 39171947
get-qaduser | ? { $_.SamAccountName.Length -eq 7 -or $_.SamAccountName.Length -eq 8 }

Open in new window

should give you all those user objects.

Author Comment

ID: 39172016
Thanks Qlemo

Any way to modify it so when I output the file it only gives me the samAccountName ?
It is outputting all the attributes below and since I have 200K plus accounts this may take longer ?

get-qaduser | ? { $_.SamAccountName.Length -eq 7 -or $_.SamAccountName.Length -eq 8 } | Export-CSV 7or8.csv

Exported all these...
objectClass      objectSid      whenCreated      objectGUID      whenChanged      edsvaNamingContextDN      l      facsimileTelephoneNumber      givenName      mail      sn      mobile      physicalDeliveryOfficeName      telephoneNumber      st      wWWHomePage      City      Company      Department      Email      Fax      FirstName      HomePhone      Initials      LastName      LogonName      Manager      MobilePhone      Office      Pager      PhoneNumber      PostalCode      PostOfficeBox      PrimaryGroupId      StateOrProvince      StreetAddress      Title      WebPage      HomeDirectory      HomeDrive      ProfilePath      LogonScript      UserPrincipalName      TsProfilePath      TsHomeDirectory      TsHomeDrive      TsAllowLogon      TsRemoteControl      TsMaxDisconnectionTime      TsMaxConnectionTime      TsMaxIdleTime      TsReconnectionAction      TsBrokenConnectionAction      TsConnectClientDrives      TsConnectPrinterDrives      TsDefaultToMainPrinter      TsWorkDirectory      TsInitialProgram      AccountExpires      PasswordLastSet      PasswordAge      PasswordExpires      LastLogonTimestamp      LastLogon      LastLogoff      AccountIsDisabled      AccountIsLockedOut      PasswordNeverExpires      UserMustChangePassword      AccountIsExpired      PasswordIsExpired      AccountExpirationStatus      PasswordStatus      DeprovisionStatus      NTAccountName      SamAccountName      Security      Domain      LastKnownParent      MemberOf      NestedMemberOf      Notes      AllMemberOf      Keywords      ProxyAddresses      PrimarySMTPAddress      PrimarySMTPAddressPrefix      PrimarySMTPAddressSuffix      PrimaryX400Address      PrimaryMSMailAddress      PrimaryCCMailAddress      PrimaryMacMailAddress      PrimaryLotusNotesAddress      PrimaryGroupWiseAddress      EmailAddressPolicyEnabled      Path      DN      CanonicalName      CreationDate      ModificationDate      ParentContainer      ParentContainerDN      Name      ClassName      Type      Guid      Sid      Description      DisplayName      OperationID      OperationStatus      Cache      Connection      DirectoryEntry
LVL 70

Accepted Solution

Qlemo earned 500 total points
ID: 39172093
Just pipe the result thru select-object prior to exporting:
get-qaduser |
   ? { $_.SamAccountName.Length -eq 7 -or $_.SamAccountName.Length -eq 8 } |
   select samAccountname | Export-CSV 7or8.csv

Open in new window

That's the same as for any other object you want to restrict the visible properties.

Author Closing Comment

ID: 39172264
Awesome ! that was it .. thanks !

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read…
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…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

617 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