none
Powershell Script to delete ADComputer Accounts RRS feed

  • Question

  • Hi There,

    I have this script which deletes a computer account from AD if found but my problem now is it throws an error " Cannot find an object property " if the account doesnt exist. I read somewhere of using the try catch and others saying its not advisable within a if else block.

    Any help will be appreciated. Thanks

    #Script to Delete Computer Accounts.

    $CptName = Read-host "Which computer accounts do you want to delete"

    #Get-Adcomputer $CptName

    If ($cptName -eq $Null)
    {
     Write-Host " This accounts does not exist " 
    }
    Else

     Remove-ADComputer -Identity $CptName
     Write-host "This computer accounts has been successfully deleted"
    }

    Thursday, April 9, 2015 9:05 AM

Answers

All replies