question

suportteche-2989 avatar image
0 Votes"
suportteche-2989 asked LimitlessTechnology-2700 edited

Script to delete duplicate computers from SCCM collection on daily basis .

Hi ,

Can any one let me know the script to delete duplicate computers from SCCM collection on daily basis , i would like to schedule task for the same. please guide me the ways for the same.

Best Regards,
Suport.Teche

windows-10-network
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered LimitlessTechnology-2700 edited

Hi,

Here's an example script you can use in this scenario:

 <#
 AUTHOR: Eswar Konetti
 DATE: 18-Nov-2016
 COMMENT : This script will read notepad file for client computers
          ,check if they exist and remove from SCCM .
 VERSION: 1.0
 #>
 # Determine script location and create log file to store the results
 $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
 $log = "$ScriptDir\clientremoval.log"
 $date = Get-Date -Format "dd-MM-yyyy hh:mm:ss"
    
 "--------------------- Script executed on $date (dd-MM-yyyy hh:mm:ss) ----------- ----------" + "`r`n" | Out-File $log -append
 #try import SCCM Module ,if error catch it.
 Try
 {
 Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)
 $SiteCode = Get-PSDrive -PSProvider CMSITE
 $SMSProvider=$sitecode.SiteServer
 Set-Location "$($SiteCode.Name):\"
 }
 catch
 {
  "[ERROR]`t SCCM Module couldn't be loaded. Script will exit!" | Out-File $log -append
  exit 1
  }
  # Read the notepad file for client records
  ForEach ($client in Get-Content $ScriptDir"\clients.txt")
   {
    $CN=Get-CMDevice -Name $client
    $name=$CN.Name
    if ($name)
    {
    try {
        "$date [INFO]`t $name found in SCCM " | Out-File $log -append
 Remove-CMDevice -name $client -force
 "$date [INFO]`t $name removed from SCCM " | Out-File $log -append
 }
 catch
 {"$date [INFO]`t $name found in SCCM but unable to delete record.Check further " | Out-File $log -append
 }
 }
    else
     { "$date [INFO]`t $client not found in SCCM " | Out-File $log -append}
    }

You can use this script as a task scheduled to run weekly. All you need to do is pipe your computer records into Notepad and allow the script to run for you automatically.

If the answer was helpful, please don't forget to vote positively or accept as an answer, thank you.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.