title: Remove computer asset from Active Directory and SCCM 2012 - Powershell
body: <p>I am trying to modify the following script to allow me to:-</p>
  
  <ol>
  <li>Read from a list of computers in a csv</li>
  <li>Delete each computer from Activedirectory &amp; SCCM 2012</li>
  </ol>
  
  <p>I have very limited powershell experience, so any help would be greatly appreciated :)</p>
  
  <p>Thanks Adi    </p>
  
  <pre><code># Environment setup 
  # Import the ActiveDirectory module to enable the Get-ADComputer CmdLet 
  Import-Module ActiveDirectory
  
  $sccmServer = SCCMServer
  $sccmSite = Site
  $computerName = Import-CSV -Path Assets.csv
  
  # find and delete the computer from AD
  ForEach ($pc in $computers) {
  $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
  $root = $dom.GetDirectoryEntry()
  $search = [System.DirectoryServices.DirectorySearcher]$root
  $search.filter = "(&amp;(objectclass=computer)(name=$computerName))"
  $search.findall() | %{$_.GetDirectoryEntry() } | %{$_.DeleteObject(0)}
  
  # find and delete from SCCM
  $comp = get-wmiobject -query "select * from SMS_R_SYSTEM WHERE Name='$computerName'" -computername $sccmServer -namespace "ROOT\SMS\site_$sccmSite"
  $comp.psbase.delete()
   }
  # spit out results
  Write-Host "Deleted $computerName from AD. Removed $computerName from SCCM server $sccmServer, site $sccmSite"
  </code></pre>