SCCM – Delete a Device using VBS
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
strComputer = InputBox("Machine Name To Delete",,"Machine Name")
Set locator = CreateObject( "WbemScripting.SWbemLocator" )
Set WbemServices1 = locator.ConnectServer( strServer,"root\SMS\site_" & strSiteCode
ResID = getResID(strComputer, WbemServices1)
If ResID = Empty Then
MsgBox "Cannot Get The ResourceId For " & strComputer & " Exiting Application"
MsgBox "The ResourceId For " & strComputer & " On " & strServer & " Is " & ResID
Set sResource = WbemServices1.Get("SMS_R_System='" & ResID & "'")
If Err = 0 Then
MsgBox strComputer & " Has Been Removed From " & strSiteCode
MsgBox "Unable To Find " & strComputer & " On " & strServer
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
For Each objInstance in objEnumerator
For Each oProp in objInstance.Properties_
GetResID = oProp.Value
Set objEnumerator = Nothing
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.
If you have any comments or questions on this post, then please feel free to leave us a comment below using our Disqus system.