SCCM – Delete a Device using VBS

sccm logo

Last updated on March 30th, 2023 at 06:04 pm

Read Time:2 Minute, 8 Second

If you are using SCCM and you want to delete a device from your database (for example if you are using OSD and only deploying to Unknown Devices) then you would normally have to go into the SCCM Console, Devices, search for the device and then right click and delete the device which would remove it from the SCCM database.

However, you can use a very simple script to carry this out to allow you to give the script to your help desk or OSD teams so that they can quickly and easily delete a device without having to pester your SCCM administrations to carry out this mundane task.

The script you can put together to use is as below:

ON ERROR RESUME NEXT strServer=InputBox("Site Server",,"TGHCM1607") strSiteCode=InputBox("Site Code",,"TGH") strComputer = InputBox("Machine Name To Delete",,"Machine Name") Set locator = CreateObject( "WbemScripting.SWbemLocator" ) Set WbemServices1 = locator.ConnectServer( strServer,"rootSMSsite_" & strSiteCode ResID = getResID(strComputer, WbemServices1) If ResID = Empty Then MsgBox "Cannot Get The ResourceId For " & strComputer & " Exiting Application" Wscript.Quit Else MsgBox "The ResourceId For " & strComputer & " On " & strServer & " Is " & ResID End If Set sResource = WbemServices1.Get("SMS_R_System='" & ResID & "'") sResource.Delete_ If Err = 0 Then MsgBox strComputer & " Has Been Removed From " & strSiteCode Else MsgBox "Unable To Find " & strComputer & " On " & strServer End If Set sResource = Nothing Function GetResID(strComputer, oWbem) strQry = "Select ResourceID from SMS_R_System where Name=" & "'" & strComputer & "'" Set objEnumerator = oWbem.ExecQuery(strQry) If Err <> 0 Then GetResID = 0 Exit Function End If For Each objInstance in objEnumerator For Each oProp in objInstance.Properties_ GetResID = oProp.Value Next Next Set objEnumerator = Nothing End Function

The script can be edited in a number of ways to customise it to your local environment. So, at the moment it is generalised so will ask you to input your site code and site server, you can change this to be hard coded so that all your help desk tech’s will have to do is to enter the machine name.

So once you are happy with the script and tested it in your lab environment (do not blame us if you do not test it and some tech deletes all your devices!) then you can save this as a VBS script and give it out as you see fit.

Comments

If you have any comments or questions on this post, then please feel free to leave us a comment below using our comments system.

Click to rate this post!
[Total: 0 Average: 0]

Free Subscription

If you want to be notified when we post more quality guides like this one, sign up to our free subscription service and you will receive an email when a new post is live.

Join 441 other subscribers.

No need to worry, we will not be filling your inbox with spam and you can unsubscribe anytime you like.


One thought on “SCCM – Delete a Device using VBS

Leave us a message...

This site uses Akismet to reduce spam. Learn how your comment data is processed.