Trying to get a count of AD users

I don't PowerShell enough, so each time I need to run a script my go to is Google, but I am not finding what I need.

I am running the following to get a count of user IDs and it's returning 0 count and all of our user IDs are 6-7 characters long, so know there are user IDs with 6 characters.

(Get-ADUser -Filter * -SearchBase “OU=Users,OU=Company,DC=MyDomain” | Where {$_.samaccountname.lenght -eq 6}).count

Once I can get this to run I need to add, or $_.samaccountname.length -eq 7 and I need to check that the account name starts with 2 alpha characters and 4-5 numbers.

Hope someone can help point me in the right direction.

Thanks, in advance.

  • First of all. You spelled length wrong.

    lenght <> Length

    Don't just use Google, go to YouTube and view the PowerShell using Active Directory videos, or use the ones on MSDN Channel9 or visit the MS for prebuilt modules / scripts or use the Windows tools that write the baseline code for you that you can tweak as needed.!q=powershell&lang=1033

    Break your search queries in to sections/step. Handle one section/step at a time, then put it all together. AS you will often never find all you want / need in a single post / response.

    If you don't use PowerShell enough, then do so. Stop dropping to cmd.exe and just start using PowerShell for everything. Anytime you find yourself thinking DOS stuff first, stop, and figure out how to do it in PowerShell.

    Learn to master the helps files as shown in the links and more below.

    Learning this stuff.

    Best Practices


    Practice with PSKoans

    PSKoans : 0.50.0
    A module designed to provide a crash-course introduction to PowerShell with programming koans.

    Virtually what you are asking is a PowerShell 101 question, and is shown in the help file examples as well.

    There are lots of solid references available. What are the best is a matter of opinion. This is why rankings on books, say on Amazon and other sites that allow user ratings vary can vary wildly. You too will have your take on the topics when you read them. My suggestion is first do some - free - live video beginner training via Microsoft virtual academy, and even on Youtube. Be sure to follow the authors blogs mentioned in the references below as well.

    Lastly, never ever, run any code from any one from anywhere, that you do not fully understand and what it is / will do. If you do, you could seriously damage your host and or your enterprise. Learn to master the -WhatIf, -Confirm, and leveraging the Set-StrictMode are part of your development, put that in your profile. Learn about profiles, and how to use them. Learn about execution policies, what they are and how / when to use them. Fully leverage auditing, monitoring and logging of PowerShell use by all users and processes. Especially as part of your corporate risk management effort.

    Work on code style, sharing and use standardization.

    Free eBook & references,

    Paid for books / references - normally the ones you'll see most recommend:

    Beginning ---

    Learn Windows PowerShell in a Month of Lunches 3rd Edition
    Donald W. Jones   (Author),‎    Jeffrey Hicks (Author)
    ISBN-13:  978-1617294167
    ISBN-10:  1617294160

    Internediate ---

    Windows PowerShell Cookbook: The Complete Guide to Scripting Microsoft's Command Shell 3rd Edition
    Lee Holmes   (Author)
    ISBN-13:  978-1449320683
    ISBN-10:  1449320686

    Advanced ---

    Windows PowerShell in Action 3rd Edition
    by Bruce Payette (Author),‎    Richard Siddaway (Author)
    ISBN-13:  978-1633430297
    ISBN-10:  1633430294

    From Microsoft

    Windows PowerShell Survival Guide

    Purpose of this Document
    The purpose of this document is to help you to learn more about PowerShell and to be successful in applying it. This document seeks to point to the best content on the web to enable you to reach that goal.

    Scope of this Document
    This page contains links to help you learn more about Microsoft Windows PowerShell. This includes PowerShell fundamentals as well as how PowerShell is used in Windows applications and services. As long as it's PowerShell related, we'll try to point to it!  The document is also version agnostic, and contains information about current and future versions of PowerShell.


    As for you query...

        # Select the 4th OU from ADDS
        ($SearchDN = (Get-ADOrganizationalUnit -Filter '*' |
        Select-Object -Property Name, DistinguishedName)[3]) |
        Format-Table -AutoSize

        Write-Host "Selecting OU $($SearchDN.Name) and the count is: " -NoNewline
        (Get-ADUser -Filter * -SearchBase $SearchDN.DistinguishedName |
        Where {($PSItem.SamAccountName).Length -gt 3}).count

        # Results
        Name     DistinguishedName             
        ----     -----------------             
        Users    OU=Users,DC=contoso,DC=com

        Selecting OU Users and the count is: 10

Reply Children
No Data