Solved

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

Posted on 2010-08-16
17
1,503 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:cpadm
  • 9
  • 5
  • 2
  • +1
17 Comments
 
LVL 1

Author Comment

by:cpadm
ID: 33446965
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...

0
 
LVL 41

Expert Comment

by:graye
ID: 33447296
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
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33447390
Why not use Robocopy, it solves all the world's issues! XD

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


0
 
LVL 1

Author Comment

by:cpadm
ID: 33447600
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?
0
 
LVL 1

Author Comment

by:cpadm
ID: 33447688
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?
0
 
LVL 41

Expert Comment

by:graye
ID: 33447841
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

0
 
LVL 1

Author Comment

by:cpadm
ID: 33448093
This is a Windows 2003 Std server.
0
 
LVL 14

Expert Comment

by:Giovanni Heward
ID: 33448275
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

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:cpadm
ID: 33448612
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

0
 
LVL 1

Author Comment

by:cpadm
ID: 33448686
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...

0
 
LVL 14

Expert Comment

by:Giovanni Heward
ID: 33449338
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

0
 
LVL 14

Expert Comment

by:Giovanni Heward
ID: 33449879
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"
0
 
LVL 1

Author Comment

by:cpadm
ID: 33450251
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?
0
 
LVL 1

Author Comment

by:cpadm
ID: 33450292
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...
0
 
LVL 14

Accepted Solution

by:
Giovanni Heward earned 500 total points
ID: 33455838
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
:logit
title %*&echo %*&echo %*>>!log!

Open in new window

0
 
LVL 14

Expert Comment

by:Giovanni Heward
ID: 33456046
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

0
 
LVL 1

Author Closing Comment

by:cpadm
ID: 33457112
Excellent script x66_x72_x65_x65!

Thank you,
Fin
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now