none
How to remove shortcut from \All Users\Desktop, when you know the shortcut's target?

    Question

  • Does anyone have a simple script for this that I can just drop my path into and make it work? I'm having trouble finding one that actually works like it should. My google-fu must be weak.

    It either needs to be a script I can attach to a current GPO (preferred), or a handful of lines I can drop into a login script that already exists.

    Monday, February 22, 2010 10:55 PM

Answers

  • Hi Haml337,

    The code sample I wrote above using the FileSystemObject should be much faster than WMI queries. In addition, hard-coding the path as '\All Users\Desktop' is valid in XP, but not in Vista and later -- that's why I used the SpecialFolders property of the WshShell object to ask for the correct directory -- this will always work regardless of platform.

    Here's a PowerShell code sample (note that the FileSystemObject object is not needed):

    $wshShell = New-Object -ComObject "WScript.Shell"
    
    $files = Get-ChildItem (Join-Path $wshShell.SpecialFolders.Item("AllUsersDesktop") "*.lnk")
    foreach ($file in $files) {
      $shortcut = $wshShell.CreateShortcut($file.FullName)
      if ($shortcut.TargetPath -eq "whatever") {
        Remove-Item $file
      }
    }

    HTH,

    Bill
    Wednesday, February 24, 2010 1:01 AM
    Moderator

All replies

  • This might seem a cheat but I THINK if you ran the Delete from a Batch file via GPO that could work.

    Failing that what is your back end for Servers?  If you have Server 2008 in the Client Side Extensions Preferences let's you add/delete shortcuts via GPO.

    What are you trying to remove from the Common Desktop?

    Sean
    The Energized Tech
    Powershell. It's so Easy and it's FREE! Dive in and use it now, It'll take no time. :) http://www.energizedtech.com http://www.itprotoronto.ca
    Tuesday, February 23, 2010 3:30 AM
    Moderator
  • Try this.  Queries WMI to find a shortcut on the all users desktop with a specific target path end (in this case Winamp.exe).  It then deletes shortcut.  I recommend testing this without calling DeleteFile first!!

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colItems = objWMIService.ExecQuery("select * from Win32_ShortcutFile where path like '%\\All Users\\Desktop%' and Target like '%\\winamp.exe'")
    
    For Each objItem in colItems
    
        Wscript.Echo "Path: " & objItem.Path
        Wscript.Echo "Target: " & objItem.Target
      Wscript.Echo "Name: " & objItem.Name
       DeleteFile(objItem.Name)
    
    Next
    
    
    Sub DeleteFile(sFile)
    
    	Set FSO = CreateObject("Scripting.FileSystemObject") 
    
    	If FSO.FileExists(sFile) Then
       		FSO.DeleteFile sFile
       
    	End If 
    
    End Sub

    • Proposed as answer by Danno.UK Tuesday, February 23, 2010 12:04 PM
    Tuesday, February 23, 2010 11:50 AM
  • Sean: When we set up computers for production, we added a shortcut to a program hosted on one of our servers. We no longer use that program, but have many computers with that shortcut to our server's program. Hoping to remove that shortcut without logging into every machine.

    Danno: How do I test without using DeleteFile? Just remove that line from the script? I have near-zero experience scripting. I thought testing would just involve running that vbs on a local machine, and then putting it in a GPO if it worked properly.
    Tuesday, February 23, 2010 2:31 PM
  • Yeah - you're right it does, but I suggest that you run without the deletefile bit first, so that you can confirm that it is finding the correct shortcuts.  Do this by:

    Comment out the DeleteFile(objItem.Name) by putting a ' infront of it.   Then just run the script with CSCRIPT, i.e CSCRIPT removeshortcut.vbs.  You should then see three bits of information displayed for each shortcut that WOULD be deleted.  Do this on a couple of computers.

    If if is finding the right shortcut then uncomment the DeleteFile(objItem.Name) bit and comment out the wscript.echo lines (again by putting a ' infront of each line) as you don't want any messages displaying.

    Hope this makes sense.
    Tuesday, February 23, 2010 3:57 PM
  • I commented out the line with a ' mark, but when I went to "Run > cscript removeshortcut.vbs" I saw a command prompt window flash open for all of half a second and disappear. I couldn't read any of the output, if there was any. Did I do something wrong? Or did I look in the wrong place?

    Wait. Tried it in a command window and came out with this:

    C:\Documents and Settings\Haml337\Desktop>cscript removeshortcut.vbs
    Microsoft (R) Windows Script Host Version 5.7
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    C:\Documents and Settings\Haml337\Desktop\removeshortcut.vbs(7, 1) (null): 0x80041017



    And this is what it's supposed to look like?

    Tuesday, February 23, 2010 5:12 PM
  • Do you mean something like this?

    Dim FSO, WshShell, Files, File, Shortcut
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set WshShell = CreateObject("WScript.Shell")
    
    Set Files = FSO.GetFolder(WshShell.SpecialFolders("AllUsersDesktop")).Files
    For Each File In Files
      If File.Type = "Shortcut" Then
        Set Shortcut = WshShell.CreateShortcut(File.Path)
        If LCase(Shortcut.TargetPath) = "whatever" Then
          FSO.DeleteFile File.Path
        End If
      End If
    Next

    By the way, don't let the CreateShortcut method's name throw you off. The method is named poorly; it also allows you to open shortcuts and look at their properties, not just create shortcuts.

    HTH,

    Bill
    Tuesday, February 23, 2010 6:30 PM
    Moderator
  • Can you show me the exact code you have now please?  And the target path of the shortcut.  Looks like it didn't find it so it may be your query is slightly out.
    Tuesday, February 23, 2010 9:39 PM
  • strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colItems = objWMIService.ExecQuery("select * from Win32_ShortcutFile where path like '%\\All Users\\Desktop%' and Target like '%\\<server>\<directory>\<program>.exe'")

    For Each objItem in colItems

        Wscript.Echo "Path: " & objItem.Path
        Wscript.Echo "Target: " & objItem.Target
        Wscript.Echo "Name: " & objItem.Name
        'DeleteFile(objItem.Name)

    Next


    Sub DeleteFile(sFile)

     Set FSO = CreateObject("Scripting.FileSystemObject")

     If FSO.FileExists(sFile) Then
         FSO.DeleteFile sFile
      
     End If

    End Sub

     




    And to test it, I saved it to the desktop, opened a cmd, then cd'd to the desktop (you saw above where I did that), and typed "cscript name.vbs" and hit enter. The results are above.
    Tuesday, February 23, 2010 10:40 PM
  • Hi Haml337,

    The code sample I wrote above using the FileSystemObject should be much faster than WMI queries. In addition, hard-coding the path as '\All Users\Desktop' is valid in XP, but not in Vista and later -- that's why I used the SpecialFolders property of the WshShell object to ask for the correct directory -- this will always work regardless of platform.

    Here's a PowerShell code sample (note that the FileSystemObject object is not needed):

    $wshShell = New-Object -ComObject "WScript.Shell"
    
    $files = Get-ChildItem (Join-Path $wshShell.SpecialFolders.Item("AllUsersDesktop") "*.lnk")
    foreach ($file in $files) {
      $shortcut = $wshShell.CreateShortcut($file.FullName)
      if ($shortcut.TargetPath -eq "whatever") {
        Remove-Item $file
      }
    }

    HTH,

    Bill
    Wednesday, February 24, 2010 1:01 AM
    Moderator
  • Because I'm still learning about code, I want to ask a few questions.


    This line: $files = Get-ChildItem (Join-Path $wshShell.SpecialFolders.Item("AllUsersDesktop") "*.lnk")
    It's looking for every shortcut inside the \All Users\Desktop\ folder, and getting their attributes, like the paths of the files they're pointing to?


    This line: if ($shortcut.TargetPath -eq "whatever") {
    Will it read -eq \\server\folder\file.exe or will it read -eq "\\server\folder\file.exe" with the quotes?


    Also, I was once told when using scripts for GPOs (which I'm going to attach this script to), it shouldn't be a vbs file, but a .cmd or .bat extension. Does this matter?
    Wednesday, February 24, 2010 3:43 PM
  • Hi,

    The code with $files is a PowerShell script. If all of your machines don't have PowerShell installed, then you can't use a PowerShell script. The PowerShell code is a translation of the following VBScript code I posted earlier:

    Dim FSO, WshShell, Files, File, Shortcut
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set WshShell = CreateObject("WScript.Shell")
    
    Set Files = FSO.GetFolder(WshShell.SpecialFolders("AllUsersDesktop")).Files
    For Each File In Files
      If File.Type = "Shortcut" Then
        Set Shortcut = WshShell.CreateShortcut(File.Path)
        If LCase(Shortcut.TargetPath) = "whatever" Then
          FSO.DeleteFile File.Path
        End If
      End If
    Next

    In the above code, you need to replace "whatever" with "\\server\folder\file.exe" (all lowercase, since we're using the LCase function). The script gets the collection of files in the "AllUsersDesktop" directory and iterates the collection. If the file's type is "Shortcut", the script opens the shortcut file as a WshShortcut object using the CreateShortcut() method. If the WshShortcut object's TargetPath property matches a path you specify, then it deletes the shortcut file.

    HTH,

    Bill
    Wednesday, February 24, 2010 4:50 PM
    Moderator
  • I am not sure if we have let Power Shell push down to all our machines. So I will use your VBS code and see how it works for us. Updates soon.



    UPDATE: Works super. So, I keep it as a VBS, and then I'll attach it to the GPO in the editor to "Comp Configuration > Windows Settings > Scripts (Startup/Shutdown)" to make it apply correctly when users log on?
    Wednesday, February 24, 2010 5:18 PM
  • I am not sure if we have let Power Shell push down to all our machines. So I will use your VBS code and see how it works for us. Updates soon.

    UPDATE: Works super. So, I keep it as a VBS, and then I'll attach it to the GPO in the editor to "Comp Configuration > Windows Settings > Scripts (Startup/Shutdown)" to make it apply correctly when users log on?
    Hi,

    Glad to hear the script works for you. If you put it in that GPO location, the script will run when the computer starts up or shuts down (recommended). If you put it as a user logon script, the script will likely fail due to insufficient permissions.

    HTH,

    Bill
    Wednesday, February 24, 2010 7:13 PM
    Moderator
  • Hi everyone,

    Sorry to hijack this thread.
    Im trying to something similair, but i want to remove a url file.

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colItems = objWMIService.ExecQuery("select * from Win32_ShortcutFile where path like '%\\All Users\\Desktop%' and Target like '%http:\\www.google.co.uk'")

    For Each objItem in colItems

        Wscript.Echo "Path: " & objItem.Path
        Wscript.Echo "Target: " & objItem.Target
        Wscript.Echo "Name: " & objItem.Name
        'DeleteFile(objItem.Name)

    Next


    Sub DeleteFile(sFile)

     Set FSO = CreateObject("Scripting.FileSystemObject")

     If FSO.FileExists(sFile) Then
         FSO.DeleteFile sFile
      
     End If

    End Sub

    I tried the above, but it doesnt seem to work with urls. I dont get any error messages.
    Can anyone hlpe with this?

    Thanks

    Allan
    Wednesday, March 10, 2010 4:51 PM
  • Hi Allan,

    Please start a new thread and be as specific as possible about what you want to accomplish.

    Thanks,

    Bill
    Wednesday, March 10, 2010 9:47 PM
    Moderator
  • Get-ChildItem $env:USERPROFILE\Desktop\*.lnk|ForEach-Object { Remove-Item $_ } ..

    How about this...??? Will this delete shortcut on desktop ..?? How do i put it in powershell ,..????

    I am no-vice in powershell scripting..Just trying to learn as its a part of my work profile.

    Thursday, July 11, 2013 9:57 AM