VB Script that will scan user profile for .lnk files, and delete

I have a mandatory profile that all Citrix users load on logon. The profile is locked down, but Windows 2003 is regenerating shortcuts in the Start menu that I do not want:

%USERPROFILE%>>programs>>Internet Explorer.lnk
%USERPROFILE%>>programs>>Outlook Express.lnk
%USERPROFILE%>>programs>>Accessories>>Address Book.lnk
%USERPROFILE%>>programs>>Accessories>>Entertainment>>Windows Media Player.lnk

 I want to run a profile startup script that will delete all these shortcuts.

I'm looking for a Script that will:

- Recursively search the %Userprofile% folders for .lnk files, delete the files, the create a log of the deleted files.


- If we can't get that working, a simple manual deletion of the above files using static paths

I've attached my code below modified from a script I found on computerperformance.co.uk (Editor: Guy Thomas)
' This code is a modified version of Guy Thomas' script on
' http://www.computerperformance.co.uk/ezine/ezine139.htm
Option Explicit
Dim objFSO, objFiles, objShell, intCount
Dim strFile, strName, strLongName, strDirectory, strEnv, strExt
Set objShell = CreateObject("Wscript.Shell")
strEnv = objShell.ExpandEnvironmentStrings("%userprofile%")
intcount = 0

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO = objFSO.GetFolder(strEnv)
Set objFiles = objFSO.Files
For each strFile in objFiles
   strExt = strFile.Type
     If lcase(strExt) = "LNK FILE" Then
     strName = strFile.Name
     strLongName = strLongName & strName & VBTab & VBTab
     intCount = intCount +1
   End if
WScript.Echo "The number of LNK files is: " & intCount
WScript.Echo strLongName & VbCr & intCount
set objFSO = nothing
set strFile = nothing
set objFiles = nothing

Open in new window

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Giovanni HewardConnect With a Mentor Commented:
Here is the final script which will meet your requirements posted above.  If you don't want to delete all .lnk files recursively in the *current user* profile, the *default users* profile, and the *all users* profile change the DIR command in the script below to specifically identify the problem .lnk files; you may place a space between each .lnk file to look for multiple files in a single instance.  Remember to enclose each path in quotes... as shown below.  This should complete this question.
@echo off
setlocal enabledelayedexpansion
set log="%userprofile%\desktop\admin.log"
call :logit Script started %date% %time%
if exist "%userprofile%\ntuser.man" call :logit * Mandatory user profile detected (%userprofile%\ntuser.man)
for /f "delims=?" %%f in ('dir "%userprofile%\*.lnk" "%userprofile%\..\All Users\*.lnk" "%userprofile%\..\Default User\*.lnk" /s/b') do (
	call :logit Located %%f, removing...
	del /q "%%f"
call :logit Script ended %date% %time%
goto :eof
title %*&echo %*&echo %*>>!log!

Open in new window

cpadmAuthor Commented:
The 'If lcase(strExt) = "LNK FILE" Then' is not working - I've tried replacing it with:
'If lcase(strExt) = ".lnk" Then' but no go either...

Why not use something simple like the following:
 del %USERPROFILE%\programs\Internet Explorer.lnk
del %USERPROFILE%\programs\Outlook Express.lnk
del %USERPROFILE%\programs\Accessories\Address Book.lnk
del %USERPROFILE%\programs\Accessories\Entertainment\Windows Media Player.lnk
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Why not use Robocopy, it solves all the world's issues! XD

Robocopy "%UserProfile%\programs" "" *.lnk /MOV

cpadmAuthor Commented:
Graye, I already have a VB script on the server that maps network drives depending on Windows security groups. I want to integrate the 2 scripts.

QCubed, I'm a big fan of Robocopy, but can you explain exactly how that switch deletes .lnk files, and will that line recursively scan all folders in %Userprofile% for .lnk files, deleting shortcuts as it locates them?
cpadmAuthor Commented:
Graye, I just tried a batch script with the above pasted lines - The script runs, but the shortcuts are still there...

Perhaps the script runs before the shortcust are generated?
Is there a ' wscript.sleep <seconds> ' equivalent for batch files?
Yeah, the path is not correct... for modern versions of Windows it'd be something like this:
%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\\programs\Internet Explorer.lnk

cpadmAuthor Commented:
This is a Windows 2003 Std server.
Giovanni HewardCommented:
Why not simply use the following batch file?
@echo off
for /f "delims=?" %%f in ('dir %userprofile%\*.lnk /s/b') do del "%%f"

Open in new window

cpadmAuthor Commented:
x66_x72_x65_x65, that script is very good, but I'm after finding out the root of my problem. I added "Sleep 10" after your 2 script lines above.

On logging on to the Citrix server, all users load a mandatory profile from C:\profile.mand. I have saved the startup batch file to C:\profiles.mand\stduser\Start Menu\Programs\Startup. The batch file executes on log on, but the following error is displayed:

"The system cannot find the path specified"

I'm guessing that for some reason, %USERPROFILE" isn't being recognised...
@echo off
for /f "delims=?" %%f in ('dir %userprofile%\*.lnk /s/b') do del "%%f"
Sleep 10

Open in new window

cpadmAuthor Commented:
If I goto Start\Run\%USERPROFILE%, my profile opens up OK.

When I open up a command prompt, and change directory to the C:\,and enter "CD %USERPROFILE%, the command executes fine, but nothing happens.

If I type "DIR %USERPROFILE%", DOS lists the directories and files in my profile...

Giovanni HewardCommented:
If your profiles are being stored in folder structure which contains a space you'll need to enclose the %userprofile% variable in quotes.

Windows 2003 profiles are stored in a folder which contains spaces (i.e. C:\Documents and Settings (by default) vs. C:\Users for Windows 2008)

You may want to experiment with the sleep command as it's likely not necessary in this case.
@echo off
for /f "delims=?" %%f in ('dir "%userprofile%\*.lnk" /s/b') do del "%%f"
sleep 10

Open in new window

Giovanni HewardCommented:
Relating to the possible root problem, have you check the following folders for the problem links?

C:\Documents and Settings\All Users\Start Menu\Programs
C:\Documents and Settings\All Users\Start Menu\Programs\Accessories
C:\Documents and Settings\Default User\Start Menu\Programs
C:\Documents and Settings\Default User\Start Menu\Programs\Accessories

Also, here are the changes to the script (bolded)

@for /f "delims=?" %%f in ('dir "%userprofile%\*.lnk" /s/b') do del "%%f"
cpadmAuthor Commented:
I'll try to explain what fixed the problem:

One of the issues was that DOS would not read %USERPROFILE% correctly unless it placed inside inverted commas. The spaces in "Documents and Settings" being the most likely reason for that.

There's a mandatory profile stored in "C:\Profile.Mand" on the Citrix server. All remote users have their terminal services profiles configured to use this profile. When I set the script up to delete the shortcuts in "%USERPROFILE%\Start Menu\Programs\..., the script looks to run OK, but the shortcuts are still available in the folders. I edited the startup login script to point at the shortcuts in the mandatory profile, and hey presto, I can see the shortcuts being deleted.

I assume this has something to do with the mandatory profile being cached every time somebody logs on?
cpadmAuthor Commented:
Another funny thing I've noticed, is that when you log on to the server via RDP, the Outlook Express shortcut is always regenerated by Windows, and can only be deleted, by:

del %USERPROFILE%\Start Menu\Programs\Outlook Express.lnk

RDP connection also load the mandatory profile, so it shouldn't be any different from logging on to the server via Citrix...
Giovanni HewardCommented:
BTW, if you want to simply clean-up all problem links from the entire drive (all users, etc.) run the following command.  This should only have to be ran once.  Obviously you may substitute "Outlook Express.lnk" for any other link name you want to recursively delete.
del /f /s /q "%SystemDrive%\Outlook Express.lnk"

Open in new window

cpadmAuthor Commented:
Excellent script x66_x72_x65_x65!

Thank you,
All Courses

From novice to tech pro — start learning today.