Link to home
Start Free TrialLog in
Avatar of Member_2_4371340
Member_2_4371340Flag for Israel

asked on

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.

OR

- 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
next
WScript.Echo "The number of LNK files is: " & intCount
WScript.Echo strLongName & VbCr & intCount
set objFSO = nothing
set strFile = nothing
set objFiles = nothing
WScript.Quit

Open in new window

Avatar of Member_2_4371340
Member_2_4371340
Flag of Israel image

ASKER

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
Avatar of Ben Personick (Previously QCubed)
Why not use Robocopy, it solves all the world's issues! XD

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


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?
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

This is a Windows 2003 Std server.
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

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

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...

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

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"
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?
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...
ASKER CERTIFIED SOLUTION
Avatar of Giovanni
Giovanni
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Excellent script x66_x72_x65_x65!

Thank you,
Fin