none
Bulk Disabling Computer Accounts RRS feed

  • Question

  • Hello,

    Looking for help with an issue I am having regarding disabling some active Computer AD accounts.

    I have a large list of computer names in a .csv file that I'd like to import and then disable in AD what I have scripted so far is not working. It is likely because I have little knowledge of how to use the 'foreach' commandlet.

    Here is my example:

    $PC = Import-CSV -Path "PathToCSV"
    foreach ($Computer in $PC) {
        Set-ADComputer $Computer -Enabled $false -Description "Disabled 11/4/20 1527hrs"
        }

    Looking for suggestions on how I can clean up this script to make it work. 

    Thanks in advance for any suggestions!


     


    • Edited by QComm89 Wednesday, November 4, 2020 9:43 PM Edit Title
    Wednesday, November 4, 2020 9:42 PM

All replies

  • If your csv has headers you have to reference the column name. Start by outputting the $computer variable to see your data.

    $PC = Import-CSV -Path "PathToCSV"
    foreach ($Computer in $PC) {
        $Computer
        }


    $PC = Import-CSV -Path "PathToCSV"
    foreach ($Computer in $PC) {
        Set-ADComputer $Computer.ServerName -Enabled $false -Description "Disabled 11/4/20 1527hrs"
        }

    Google Earth csv example.

    Wednesday, November 4, 2020 10:32 PM
  • Hi,

    In this case, targeting a Domain Controller will a good idea.
    And to make it simple, you can copy the computer names to a text file and use the code below.

    Here Domain controller - FabDC1 and C:\ComputerNames.txt contains the Computer names.

    Get-Content C:\ComputerNames.txt | ForEach-Object { 
        $Description = "Disabled at $(Get-Date)"
        Set-ADComputer -Identity $_ -Server FabDC1.fabrikam.com -Enabled $false -Description $Description
    }





    • Edited by Suman Bhowmik Thursday, November 26, 2020 10:25 AM grammer
    Thursday, November 26, 2020 10:22 AM