Recently I had a requirement to create a PowerShell script to read text file/CSV for list of clients (servers mainly) and check if they appear in SCCM if so delete them ,track the information into log file for reference.
There could be various reasons why do you want to delete computer record from SCCM and for that, you can either delete the record directly from the console or use scripting to do it for you.
One of of the reason for me to create this script is ,to help the team to delete the server records from SCCM after they decom immediately using task scheduler. Why immediate ? can't I wait for default maintenance tasks to remove the computer object after it becomes inactive or aged ? well ,it does . Once the computer record is disabled in AD or not online for X days ,It becomes inactive . Once the client is inactive and fall into the range X days to match site maintenance tasks then it will be removed from SCCM database . When I leave these decom clients for deletion using site maintenance tasks ,these decom servers do appear in compliance reports and client health reports in SCCM and because of this ,the client health dashboard doesn’t appear in good status.
So ,this script will help guys to pipe the computer records into text file (as input ) and run the script or can schedule the script to run weekly once or so. The script will read the text file ,check if the computer record exist in database or not ,if not ,out-put to log ,if exist ,delete the entry ,out-put results to log and this loop continue until the last line read in text file.
Removing a device client manually deletes the client record from the Configuration Manager database. Typically, you should not use this action unless it is for troubleshooting scenarios. If you delete the client record and the Configuration Manager client is still installed and communicating with Configuration Manager, Heartbeat Discovery recreates the client record the Configuration Manager database, although the client history and any previous associations are lost.
To delete computer record from SCCM if exist ,I used inbuilt SCCM powershell cmdlet called Remove-CMDevice -name $comp –force .
You can download Powershell script to delete computer records from SCCM via TechNet .
You can use this script as schedule task to run on weekly basis .All you need is ,pipe the computer records into the notepad and let the script run for you automatically.
12 Comments
Thank you for this! This is exactly what I needed!
Just a note, I had to change 1 small thing. Regarding line 17, in my environment, $env:SMS_ADMIN_UI_PATH resolves to C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386; however, ConfigurationManager.psd1 is located in C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386 (so one level up in the bin folder instead of i386). So, I hardcoded the path for the import-module command on line 17. All else worked exactly how I needed in my environment without changing anything.
Thanks for the update MelQ. Appreciate it. With the changing of the configmgr and how they install folder structure ,i did not update the post for a while.
Regards,
Eswar
Hi Eswar,
I believe I messaged you already but have not gotten a response. The link to the script is not valid anymore. Did you have a copy of the script where you can post it to?
Thanks,
Dee
Hi,
it is available in github https://github.com/eskonr/MEMPowered/blob/master/Technet%20Gallery/Powershell%20Delete%20computer%20records%20from%20SCCM.zip
Thanks,
Eswar
I just checked that we can use "task scheduler" for automating process. i need help with file name input (network location & file name) and changing site name/site code.
you can edit the powershell script ,change the sitecode . Follow the blog post http://eskonr.com/2014/08/sccm-configmgr-2012-powershell-script-cleanup-duplicate-obsolete-and-outdated-computers-via-scheduled-tasks/
Regards,
Eswar
Hi Eswar, Can we use the script on the machine which doesnt have configuration manager console?
Hi Sunil,
Yes but you need to connect to remote server to load the configuration manager module to load the powershell cmdlets.
Regards,
Eswar
Hi Eswar.
This is Vinay.. i am supporting SCCM 2012 and as well 2016. I got your script and i am unable to run this script i am getting error message. I need to use this script to my environment.
The String is missing the terminator:”.
+CategoryInfo :Parsererror” (:) [] ParentContainserrorRecordException
+FullyQualifiedErrorID :TerminatorExceptedAtEndofString
Hi Eswar,
This is a nice script and works well for me. If I want to delete a specific list of machines that start with XYZ, how can I pass the like condition in this script?
Hi,
You can create a collection (query) to list devices that start with xyz (xyz%) and delete the members from the collection would be easier.
Thanks,
Eswar
Need Help in modifying script according to our environment,
1. Where to mention sitename and site code in the script
2. I want to run this script daily...where to do that
3. Can i use a network location to read the txt files.
4. There is a network folder in which all the text files will be saved with name "mm-dd-yyyy.txt", in this case if i automate the script to run daily...how can i input the filename?