none
Powershell Script - Checks the existence of accounts RRS feed

  • Question

  • Hello All,

    I am trying to create a script that will verify the existence of specific Active Directory Accounts (using a imported CSV file).  If the script finds the account, it will check to see if the account is disabled.  The result I am looking for is an exported CSV file that contains all of the accounts and something in the CSV that says whether the account exists or not.  If the account does exist, is that account active or disabled.

    Here is what I have so far:

    Import-Csv ListofUsers.csv | ForEach-Object {
        New-Object -TypeName PSCustomObject -Property @{
            samaccountname = $_.samaccountname
            exist = [bool]($account=([adsisearcher]"(samaccountname=$($_.samaccountname))").findone())
            enabled = [bool]($account.properties.useraccountcontrol[0] -band 2)
            Disabled = [bool]($account.properties.useraccountcontrol[0] -band 2)
        }
    } | Export-Csv Output.csv -NoTypeInformation

    Monday, December 11, 2017 8:18 PM

Answers

  • if($account=([adsisearcher]"(samaccountname=$($_.samaccountname))").findone()){
         # account exists
     }else{

          #account doesn't exist
     }


    \_(ツ)_/



    • Edited by jrv Monday, December 11, 2017 8:52 PM
    • Marked as answer by giant45man Tuesday, December 12, 2017 3:03 PM
    Monday, December 11, 2017 8:51 PM

All replies

  • You have to get the account and test it first or you will get errors.


    \_(ツ)_/

    Monday, December 11, 2017 8:30 PM
  • I did test this. It works except that it doesn't list the accounts that do not exist.  Can you help me with that piece?
    Monday, December 11, 2017 8:38 PM
  • You have to check if the account exists before you create the object.

    Where did you copy this from?


    \_(ツ)_/

    Monday, December 11, 2017 8:42 PM
  • I am not sure where I copied this from.  

    What code can I use to see if the account exists?

    Monday, December 11, 2017 8:45 PM
  • if($account=([adsisearcher]"(samaccountname=$($_.samaccountname))").findone()){
         # account exists
     }else{

          #account doesn't exist
     }


    \_(ツ)_/



    • Edited by jrv Monday, December 11, 2017 8:52 PM
    • Marked as answer by giant45man Tuesday, December 12, 2017 3:03 PM
    Monday, December 11, 2017 8:51 PM
  • I will try this.  Thank you for your help.
    Monday, December 11, 2017 8:58 PM