power shell /vb script to delete duplicate computers from sccm

suport.teche 1 Reputation point
2021-09-17T16:18:45.197+00:00

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 Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,201 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,391 Reputation points
    2021-09-17T19:40:32.997+00:00

    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.

    0 comments No comments