Deleting an expired shortcut from several remote workstations

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
operez
Posts: 14
Last visit: Fri Mar 08, 2013 5:56 am

Deleting an expired shortcut from several remote workstations

Post by operez »

I need to delete an expired shortcut icon from several remote computers. I will be reading a text file with each workstation’s IP address and performing the following function. To test my logic I was running this script with just one IP address, but it doesn’t seem to find anything, even though if I do a search of this workstation's profile folder it finds three shortcuts, one in the administrator’s folder and the other two in two user folders.

Could someone please look at this and tell me where I went wrong?


DeleteExpiredShortcuts "10.227.201.261","Lotus Notes 6.4"

Function DeleteExpiredShortcuts(strComputer,strShortcut)
  Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")

  Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_ShortcutFile Where FileName = '" & strShortcut & "'")

  For Each objItem in colItems
    If Instr(LCase(objItem.Name), "desktop") Then
      strPath = objItem.Name
      strPath = Replace(strPath, "", "")
      Set colFiles = objWMIService.ExecQuery _
        ("Select * From CIM_Datafile Where Name = '" & strpath & "'")
      For Each objFile in colFiles
        WScript.Echo "Deleting " & objFile.Name
        'objFile.Delete
      Next
    End If
  Next
  strComputer = ""
  strShortcut = ""
End Function

Thank you in advance.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Deleting an expired shortcut from several remote workstations

Post by jvierra »

Some profiles are in All Users Profile and some are in the registry.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Deleting an expired shortcut from several remote workstations

Post by jvierra »

Start here:

DeleteExpiredShortcuts "10.227.201.261","Lotus Notes 6.4"

Function DeleteExpiredShortcuts(strComputer,strShortcut)

Set wmi = GetObject("winmgmts:" & strComputer & "rootcimv2")

Set colItems=wmi.ExecQuery("Select * From Win32_ShortcutFile Where FileName = '" & strShortcut & "'")

For Each objItem in colItems
If Instr(LCase(objItem.Name), "desktop") Then
WScript.Echo "Deleting " & objItem.Name
'objItem.Delete
End If
Next

End Function
User avatar
operez
Posts: 14
Last visit: Fri Mar 08, 2013 5:56 am

Deleting an expired shortcut from several remote workstations

Post by operez »

Thank you. That seems to have worked.
This topic is 11 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked