none
Powershell Det-ADuser - When using a finite list, I still get all users in results RRS feed

  • Question

  • When I run this script I keep getting an output of all accounts in the domain and not just the finite list of names.

    Details:
    List has last names only, there are in some cases multiple last names with different first names.  I need to produce the Samaccountnames, and email for the report by only searching for the last names. I need to use the wild card because I am searching by last name.

    Problem: I end up with information on every account in AD, even though the foreach is only 253 items long.

    Curerntly my script is as follows:

    $users = get-Content "C:\Users\Downloads\Project\ADuserLookup\Users.txt"
    $expath = "C:\Users\Downloads\Project\ADuserLookup\userInfo.txt"
    
        Foreach ($user in $users) {
       
            $Value = "$user*"
            #Get-ADComputer -Filter {Name -Like $Value} | Select Name)
            Get-ADUser -Filter {Name -like $Value} | select-object SamAccountname | export-csv -path $expath -Append
        }


    Michael


    • Edited by Arkangel30 Thursday, June 11, 2015 8:58 PM
    Thursday, June 11, 2015 8:57 PM

Answers

  • Problem solved:

    The list contained names that consisted of 2-3 letters, so it was picking up almost every name that started with those to letters.

    I have removed the offending variables and can now at least manage the results.


    Michael

    • Marked as answer by Arkangel30 Thursday, June 11, 2015 10:23 PM
    Thursday, June 11, 2015 10:23 PM

All replies

  • Problem solved:

    The list contained names that consisted of 2-3 letters, so it was picking up almost every name that started with those to letters.

    I have removed the offending variables and can now at least manage the results.


    Michael

    • Marked as answer by Arkangel30 Thursday, June 11, 2015 10:23 PM
    Thursday, June 11, 2015 10:23 PM
  • You are thinking too hard.  You will burn out your brain.  Don't think.  Use Help.  Phone a friend.

    $expath='C:\Users\Downloads\Project\ADuserLookup\userInfo.txt'
    
    get-Content C:\Users\Downloads\Project\ADuserLookup\Users.txt |
        Foreach-Object{
            Get-ADUser -Filter "Surname -eq '$_'" -properties Displayname
        } |
        select-object Name, DisplayName, SamAccountname | 
        export-csv -path $expath -NoType


    \_(ツ)_/



    • Edited by jrv Thursday, June 11, 2015 10:30 PM
    Thursday, June 11, 2015 10:27 PM
  • Hi Jrv,

    This would work however it gives a lot of duplicates as people have the same last names.

    I did use the idea of "surname" to look at the names that were not found, and this helped determine that the accounts definately did not exist.

    Thanks for the insight!


    Michael

    Thursday, June 11, 2015 11:44 PM
  • A given in AD.  You cannot resolve names to a single account.  It is not possible.

    \_(ツ)_/

    Thursday, June 11, 2015 11:50 PM
  • You can also do this:

    select-object Name, DisplayName, SamAccountname |  sort samaccountname -unique



    \_(ツ)_/

    Friday, June 12, 2015 2:17 PM