Some articles are posted as Images, Please use Computers to go through them for best experience. For phone users, switch to Web Version

AD Script - Remove Computer Objects from Active Directory

<#Created By - Abhishek Bansal

Script Usage - Deleting Computer Objects mentioned in Input.txt from AD.

Incase of Access Denied, run ISE as Administrator & make sure account used should have sufficient rights to delete a Computer Object.

For using it on any other server, just copy the entire folder, edit .ps1 into PS ISE & run it. 

#>

$servers = Get-Content -Path .\Input.txt

$line = 0

$linecount = $servers.Count

$percentagecomplete= 0

$filename = "Output_"+(Get-Date -Format "yyyy_MM_dd")+".csv"

foreach($server in $servers)

{

$error.Clear()

$server = $server.trim()

$line++

$percentagecomplete = $line / $linecount * 100

Write-Progress -Activity "Removing Computer Objects.." -PercentComplete $percentagecomplete -Status "$line out of $linecount"

    try{

    Remove-ADComputer -Identity $server -Confirm:$false

    $server | Select-Object -Property @{n="Computer Name";e={$server}},@{n="Status";e={("Deleted Succesfully")}} | Export-csv ./$filename -NoTypeInformation -Append

    }

    catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

      {

    $server | Select-Object -Property @{n="Computer Name";e={$server}},@{n="Status";e={$error.exception.Message}} |  Export-csv ./$filename -NoTypeInformation -Append

      }

}


No comments:

Post a Comment