Answer for:

Computer removal in AD

Message 21 of 20

View entire thread
0 Votes

Here is a basic VB script that will do the job. You will have to tailor it to your needs though.
On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\Files\ws.txt", ForReading)

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline

Set objComputer = GetObject("LDAP://CN=" & strComputer & _
"," & "OU=WorkStations,DC=myCompany,DC=com")
objComputer.DeleteObject (0)
If Err.Number = 0 Then
WScript.Echo "Deleted computer " & strComputer & " from AD"
Elseif Err.Number <> 0 Then
WScript.Echo "Unable To delete computer " & strComputer
End If

Set objTextFile = Nothing
Set objFSO = Nothing
Set objComputer = Nothing
Set objContainer = Nothing

Wscript.Echo "Done"


Their is a lot you can do including saving each computer account name to a text file to read later if you remove the wrong PC. But this is the basic code for removing computer accounts.