Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-16
17
Medium Priority
?
1,542 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 14
ID: 33447390
Why not use Robocopy, it solves all the world's issues! XD

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


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 15

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

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 15

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 15

Accepted Solution

by:
Giovanni Heward earned 2000 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 15

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

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

772 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