Diasble an AD Account

Glenn Maxwell 10,146 Reputation points
2021-11-06T03:46:23.47+00:00

Hi All

i have below AD users in a csv file, the file is in below format. i want to import this csv file and disable all these account in AD. experts guide me.

users
user1
user2
user3

will the below syntax work.

$users=Get-Content c:\temp\users.csv
ForEach ($user in $users)
{
Disable-ADAccount -Identity $($user.name)
}

After disabling i want to import the csv file for checking the status i want to export output to csv file, how would i get that information, something like this i want to try

$users=Get-Content c:\temp\users.csv
ForEach ($user in $users)
{
Get-ADUser -Identity name,enabled | export-csv c:\temp\output.csv -NotypeInformation
}

Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,470 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,378 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,898 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,091 Reputation points
    2021-11-06T14:41:40.667+00:00

    This bit of your code will never work:

    $users=Get-Content c:\temp\users.csv
     ForEach ($user in $users)
     {
         Disable-ADAccount -Identity $($user.name)
     }
    

    Each "$user" object in the "$users" array is just a simple string. A string object has no property named "name". The result is that you're trying to use $null as the value for the -Identity parameter value.

    What sort of identity have you placed in your file (users.csv)? Acceptable values would be one of these:

    A distinguished name
    A GUID (objectGUID)
    A Security Identifier (objectSid)
    A SAM Account Name (SAMAccountName)

    With an appropriate identity in the CSV column named "name", this should produce the results you want:

    Import-Csv c:\temp\users.csv |
        ForEach-Object {
            Disable-ADAccount -Identity $user.name -PassThru |
                Select-Object SamAccountName,enabled
        } | Export-Csv c:\temp\output.csv -NotypeInformation
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,926 Reputation points MVP
    2021-11-06T09:39:17.02+00:00

    Hi @Glenn Maxwell ,

    disabling the users should work like you posted in your question.
    Best for posting code in Q&A is to use the "Code Sample" icon in the Q&A editor. If you post code in Q&A in normal text format Q&A will screw up the code very often.

    To get the user details in a CSV file take a look here: https://www.alitajran.com/export-ad-users-to-csv-powershell/

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Glenn Maxwell 10,146 Reputation points
    2021-11-06T11:39:35.29+00:00

    i am getting error, is the syntax correct

    name
    user1
    user2
    user3

    $users=Get-Content c:\temp\users.csv
    ForEach ($user in $users)
    {
    Disable-ADAccount -Identity $($user.name)
    }
    
    $users = import-csv c:\temp\users.csv
    $results = ForEach ($user in $users){
               Get-ADUser -Identity $user.name| select SamAccountName,enabled
    }
    $results | export-csv c:\temp\output.csv -NotypeInformation
    
    0 comments No comments

  3. Andreas Baumgarten 96,926 Reputation points MVP
    2021-11-06T12:24:46+00:00

    Hi @Glenn Maxwell ,

    first of all it would be helpful to see the error message ;-)

    Why are you using Get-Content in the first and import-csv to do the same ... reading the csv file?

    Try import-csv in the first line as well. If you run all in one script you can just delete line 7``because you already have $usersfilled inline1`.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments