Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Vista/7, if I try to delete a shortcut using the following command -:

del "%allusersprofile%\Desktop\MyShortcut.lnk"

...Windows sees this folder as empty and doesn't delete the file.

The environment variable "allusersprofile" points to "C:\ProgramData" however "Desktop" is actually a soft symbolic link to the C:\Users\Public\Desktop folder.

The problem seems to be that these soft links are simply Window Explorer shortcuts and are not recognized by cmd prompts or batch files.

The only solution that I can see is to do the following -:

XP:

del "%allusersprofile%\Desktop\MyShortcut.lnk"

Vista/7:

del "%PUBLIC%\Desktop\MyShortcut.lnk"

Is there any common solution for both OSes?

share|improve this question
2  
I'm not aware of any such way due to the file structure changes between XP and Vista/7. One way you could achieve this functionality in a script is to get the OS version using ver, run it through a series of if/else checks, and then use goto to run the appropriate command. – Garrett Nov 28 '11 at 5:02
up vote 5 down vote accepted

As stated by Garrett in comments of this question, the only solution I see is as follows:

SET Version=XP

VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=7

IF %Version% EQU 7  (
 del "%PUBLIC%\Desktop\MyShortcut.lnk"
)
IF %Version% EQU XP  (
 del "%allusersprofile%\Desktop\MyShortcut.lnk"
)

One might note that according to this StackOverflow question, and a blog post by Raymond Chen, a dir of %allusersprofile%\Desktop\<directory> should give the proper results on both XP and 7, however in my experience it does not.

share|improve this answer

You didn’t specify a type of script (VBS vs. BAT), but here is a VB script that is system agnostic. Not my script, I pulled it from this Microsoft site. According to that page it’s been verified to work on Windows 2000, XP, Vista, and 7.

'''''''''''''''''''''''''''''''''' 
'  
' This VB script removes the requested desktop shortcuts 
'  
' Change only the file name (test.lnk) 
' 
' Script created by Holger Habermehl. October 23, 2012  
'''''''''''''''''''''''''''''''''' 
Set Shell = CreateObject("WScript.Shell") 
Set FSO = CreateObject("Scripting.FileSystemObject") 
DesktopPath = Shell.SpecialFolders("Desktop")
FSO.DeleteFile DesktopPath & "\test.lnk"

EDIT

The above code will look at the specific user's desktop (i.e. Username "john" logs in, the code will look at "C:\Users\john\Desktop\" or "C:\Documents and Settings\john\Desktop"). If you want to check the public desktop, then change the line that reads

DesktopPath = Shell.SpecialFolders("Desktop")

to

DesktopPath = Shell.SpecialFolders("AllUsersDesktop")

But note that depending on the user's privileges and when you run the script, they may get a UAC box asking to sign in as an admin on Windows Vista/7. I'd run the script in a GPO as a computer startup script.

share|improve this answer

Good advice here which helped with my scenario.

  1. I created a batch file to remove the short-cuts

fixme.bat contains the following 3 lines:

del "C:\Users\Public\Desktop\gVim 7.4.lnk"
del "C:\Users\Public\Desktop\Cygwin64 Terminal.lnk"
pause
  1. Right-mouse click on the batch file to pop-up the menu.

  2. Select "Run Elevated Privileges", enter your password.

Success.

You may also try "Run as administrator".

Good luck!

share|improve this answer
1  
Hi Gooofy, thanks for the answer. Does this cover both XP and Vista/7, as stated in the original question? – bertieb Jul 8 '15 at 16:59

This works in win7.

I haven't been able to try it in XP but I think it should work.

    del "%HOMEDRIVE%%HOMEPATH%\Desktop\test.lnk"

Save it as a batch file and run it normally. If your account doesn't have administrator privileges you may need to right-click and select "run as administrator." You could also open cmd and just type it as a command.

share|improve this answer
1  
That command is deleting a different file ... %HOMEDRIVE%%HOMEPATH% is not the same as %allusersprofile% – DavidPostill Jul 1 at 20:33
    
It still goes to the desktop though. %allusersprofile% is different but I thought %HOMEDRIVE%%HOMEPATH% might work for what he was asking. @DavidPostill – theGiantMidget 2000 Jul 3 at 18:57
    
One is the desktop for all users and the other is for a specific user. The contents are combined to create the desktop for the logged in user. – DavidPostill Jul 3 at 18:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .