locked
Power shell script to match samaccountnames in csv RRS feed

  • Question

  • Good morning everybody!

    I have a csv file which contains a list of samaccountnames which I want to match against the active directory, to see which accounts are active/inactive. 

    The csv file imported correctly into powershell, but after importing I do no know how to match this against the active directory.

    I think I may be able to use the Get-ADuser commandlet for it but I'm not sure. 

    Wednesday, July 31, 2019 9:02 AM

All replies

  • Hi,

    can you please post what you have done so far, so that we can give you more accurate suggestions.

    Basically you need to do a foreach loop and enumearte all SamAccountNames coming from ther CSV and do the Get-ADUser check within the loop.

    Thanks in advance!

    Regards,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov

    Wednesday, July 31, 2019 11:04 AM
  • Hi WSRamsoedh,

    Lets say this is the csv file:

    Username

    -----------

    UserA

    UserB

    The logic in the script should go like this.

    $users = Import-CSV "C:\Users..."
    
    foreach ($user in $users){
    
    $user = Get-ADUser -Identity $user.username -Properties *
    
    }

    In the first step we import all the users from the CSV file. 

    Please enter the full path to the CSV there.

    Then we run a foreach cycle to go through all the users in the $users variable.

    Last step would be to get the current user from AD. 

    Please note that the $user.username refers to the column name from the CSV file.

    After that you can run an if cycle inside and check if the user is enabled and disabled and run some output or export the results into a CSV.

    Hope this helps !


    Always remember to mark helpful posts as answers so they might help others too! Regards, Caki Teofilovski

    Wednesday, July 31, 2019 11:43 AM
  • Hi WSRamsoedh,

    Lets say this is the csv file:

    Username

    -----------

    UserA

    UserB

    The logic in the script should go like this.

    $users = Import-CSV "C:\Users..."
    
    foreach ($user in $users){
    
    $user = Get-ADUser -Identity $user.username -Properties *
    
    }

    In the first step we import all the users from the CSV file. 

    Please enter the full path to the CSV there.

    Then we run a foreach cycle to go through all the users in the $users variable.

    Last step would be to get the current user from AD. 

    Please note that the $user.username refers to the column name from the CSV file.

    After that you can run an if cycle inside and check if the user is enabled and disabled and run some output or export the results into a CSV.

    Hope this helps !


    Always remember to mark helpful posts as answers so they might help others too! Regards, Caki Teofilovski

    Why would he need all properties from get-aduser cmdlet?

    Why are u storing the results to the variable?  What are u doing with the variable then?  What about try/catch, if/else ...    u like red error text?

    All the OP needs to do is not to be lazy, run a single query on google  (check aduser csv or something like that) and there are thousands of examples, since this is very common task.

    Wednesday, July 31, 2019 12:42 PM
  • This is really a very simple question which all new users and many experienced users fail to understand.

    All user that exist in AD.

    Import-CSV C:\Users.csv |
        Where{Get-ADUser -Filter "SamAccountName -eq '$($_.username)'" }
    



    \_(ツ)_/


    Wednesday, July 31, 2019 4:17 PM
  • Same pattern but report on found and not found.

    Import-CSV C:\Users.csv |
        ForEach-Object{
            if(Get-ADUser -Filter "SamAccountName -eq '$($_.username)'"){
               Write-Host "User found $($_.username)" -fore green
            }else{
               Write-Host "User NOT found $($_.username)" -ForegroundColor red
            }
        }


    \_(ツ)_/

    Wednesday, July 31, 2019 4:21 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, August 1, 2019 7:34 AM