Few of the articles are posted as Images, Please use Laptop / Computers to go through the articles for best experience. For phone users, switch to Web Version

Please Share with your colleagues if you found these blogs informative. Happy Learning :-)

Powershell Script - Remove Computer Objects from Active Directory

#Created By - Abhishek Bansal

<#Read Me !! 
1. Script Usage - Deleting Computer Objects mentioned in Input.txt from AD.
2. Incase of Access Denied, run ISE as Administrator & make sure account used should have sufficient rights to delete a Computer Object.

For using it, directly copy the entire code, save it in .ps1 extension and have a Input.txt file on the same location. #>


$srv = Get-Content -Path .\Input.txt
$line = 0
$linecount = $srv.Count
$percentagecomplete= 0

foreach($row in $srv)
{
$error.Clear()
$row = $row.trim()
$line++
$percentagecomplete = $line / $linecount * 100

Write-Progress -Activity "Checking Status.." -PercentComplete $percentagecomplete -Status "$line out of $linecount"


    try{
    Remove-ADComputer -Identity $row -Confirm:$false
    $row | Select-Object -Property @{n="Computer Name";e={$row}},@{n="Status";e={("Deleted Succesfully")}} | Export-csv ./Output.csv -NoTypeInformation -Append
    }
       catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
       
       {
    $row | Select-Object -Property @{n="Computer Name";e={$row}},@{n="Status";e={$error.exception.Message}}| Export-csv ./Output.csv -NoTypeInformation -Append
    }

}

No comments:

Post a Comment