Link to home
Start Free TrialLog in
Avatar of Ken Thompson
Ken Thompson

asked on

VB Script to delete link from currently logged on user's desktop not working....

I have a VB script that I want to adapt to remove a desktop link from the currently logged on user's Desktop.  It isn't working and I get no error messages - no nothing.

On Error Resume Next
Dim FSO, WSHShell, Regkey, Username, strFolder, UserPath, objShell

Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 

Set FSO = CreateObject("Scripting.FileSystemObject")
UserPath = "C:\Users\" & Username 
strFile = "C:\Users\" & Username & "\Desktop\CMS Supervisor*.lnk}" 
if FSO.FileExists(strFile) = True then  FSO.DeleteFile DesktopPath & "\CMS Supervisor*.lnk"

Open in new window

Avatar of Ken Thompson
Ken Thompson

ASKER

Tried to use the code box but it didn't work?
Avatar of Bill Prew
I don't see you ever setting DesktopPath to a value?

Also, do you want the trailing curly brace on this line?

strFile = "C:\Users\" & Username & "\Desktop\CMS Supervisor*.lnk}"

Those were a few of the quick things, will read it further...


»bp
Are you sure this file exists?

strFile = "C:\Users\" & Username & "\Desktop\CMS Supervisor*.lnk}"


»bp
Good catch with the } Bill.  Not supposed to be there I don't think.

Also I'm using the If in this line to see if the file exists or not:

if FSO.FileExists(strFile) = True then  FSO.DeleteFile DesktopPath & "\CMS Supervisor*.lnk"

If it does I want to delete it.

Is there a better way to run this?  I'm trying to define DesktopPath now...
Tried this and it still didn't work...no error messages no nothing.....

'On Error Resume Next
Dim FSO, WSHShell, Regkey, Username, strFolder, UserPath, objShell

Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 

Set FSO = CreateObject("Scripting.FileSystemObject")
UserPath = "C:\Users\" & Username
DesktopPath = "C:\Users\" & Username & "\Desktop" 
strFile = "C:\Users\" & Username & "\Desktop\CMS Supervisor*.lnk" 
if FSO.FileExists(strFile) = True then  FSO.DeleteFile DesktopPath & "\CMS Supervisor*.lnk" 

Open in new window

Also - if anyone knows how to do this with a Powershell script I would be very interested in knowing how that works.  I'm trying to move from VB to Powershell because I know enough VB to hack things together.  I'm lost with Powershell but it works better with SCCM.
strFile contains a wildcard. FileExists doesn't support wildcards.
DeleteFile supports wildcards.
Note that the user's profile is not necessarily "C:\Users\<UserName>".
In VBScript, the path to the desktop of the currently logged on user can be obtained by querying the REG_SZ value "Desktop" in
"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders".

In PowerShell, you can use the API to query for the correct path.
And there's not even a need to check if the file(s) exist - when you use a wild card in Remove-Item, and it doesn't find a match, it just does nothing.
And there's the nice possibility of simulating, inherent to most cmdlets doing destructive or potentially fatal change: the argument "-WhatIf".
So the script below is in test mode and will not delete anything. Remove the -WhatIf to run it for real.
$desktop = [Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop)
Remove-Item -Path "$($desktop)\CMS Supervisor*.lnk" -WhatIf

Open in new window

You could actually put that into a single line, but it's spread out for clarity.
Hi,

oDBA beat me to the PS version which is always going to be shorter & neater.
I am rusty on Vsbscript but found a few issues with the original. First, it will help to always put option explicit as the first line to catch undeclared variables. Next don't enable On Error until ready for production. It will just hide errors.
Next desktoppath was used but not declared and some vice versa. See below. Then getting the user path from LogonUI looks cumbersome, so I changed it to be just an environment variable. If you are running this with SCCM mind, it will run as Local System so might not read the profile. Test it and see. Finally you had a wildcard as the filename and again as the delete target. VBscript can't handle that. Testing with just 1 file worked and I've added output. Again SCCM won't display this either. Running with
cscript "Delete LNK.vbs">deleted.log
will pipe the output to a log.
If you need to delete files with a wildcard then you probably need a loop. All that said I would just forget it and use PowerShell from oDBA. It's cleaner and works well with SCCM.

Option Explicit
'On Error Resume Next
Dim FSO, WSHShell, Regkey, Username, strUsrProfile, UserPath, strFile

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("WScript.Shell")

'RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
'Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
'Username = Mid(Username,7)
strUsrProfile = WSHShell.ExpandEnvironmentStrings("%UserProfile%")
UserPath = strUsrProfile & "\Desktop"
strFile = UserPath & "\CMS Supervisor1.lnk"
if FSO.FileExists(strFile) = True Then
      WScript.Echo "deleting file " & strFile
      FSO.DeleteFile strFile
End If

'Variables never used but declared
'objShell
'strFolder

'use but not declared
'DesktopPath


Mike
Hey oBdA,

I tested the Powershell script you provided and it works like a charm when I run it alone.  I inserted it into my script (.bat) and it doesn't run.  Note that the other call out to a Powershell script works but not the line I put in for removing the icon.

Did I typo or put the call to RemoveOldDesktopLink.ps1 in the wrong place?  Not sure what I've goofed up on.

@echo off

REM Check for existing CMS - remove if present

IF EXIST "C:\Program Files (x86)\Avaya\CMS Supervisor R18\ACSRun.exe" goto Uninstall

:Uninstall

IF EXIST "C:\Users\Public\Public Desktop\CMS Supervisor R18 -- English.lnk" goto DeletePublicLink

:DeletePublicLink

REM Delete the link in d:\Users\Public\Desktop

del "C:\Users\Public\Public Desktop\CMS Supervisor R18 -- English.lnk"

%~dp0\SetupSup_MB06.exe /uninst --silent

REM Install CMS 18.1

%~dp0\SetupSup_MB06.exe --silent

REM Copy CMS R18 (18.1) to desktop

copy /Y "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Avaya\CMS Supervisor R18\CMS Supervisor R18 -- English.lnk" "%PUBLIC%\Desktop\CMS R18.lnk"

REM Add both servers - 

PowerShell.exe Set-ExecutionPolicy -ExecutionPolicy Unrestricted
PowerShell.exe -windowstyle hidden -File %~dp0Write-Reg-Curent-User.ps1 -RegFile %~dp0CMS-Both-Servers.reg -CurrentUser
PowerShell.exe -windowstyle hidden -File %~dp0RemoveOldDesktopLink.ps1
PowerShell.exe Set-ExecutionPolicy -ExecutionPolicy Restricted


exit /b 0

Open in new window

You saw the remark that the script is in test mode, and removed the -WhatIf in the second line?
I did.  Here is the actual script contents.  

Does this have something to do with it being run from SCCM?  I've had this happen before due to the account SCCM uses for install.  It uses the System account.  Do I need to add something to tell it to do this for the Current User desktop like the one above it?

$desktop = [Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop)
Remove-Item -Path "$($desktop)\CMS Supervisor*.lnk"

Open in new window

Hi Ken,

Test the script with a scheduled task using "local system" or sysinternals tool. The desktop path looks fine running interactively but yes, local system might not read it; the only way to know is see it working. The other option is to add lots of logging to the script which is probably a good thing anyway.

Mike
Thanks Mike.

I can't get anything to work for some reason.  If I use Powershell I have to get it to find the currently logged on user (HKEY_CURRENT_USER) first and then delete the link on that user's desktop.  Still looking for how to get the current user with powershell and delete from that Desktop.  VB script doesn't work - no errors or anything.

I tried this VB that I've used successfully in the past and it isn't working either.

Dim FSO, WSHShell, Regkey, Username, strFolder, UserPath, objShell

Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 

Set FSO = CreateObject("Scripting.FileSystemObject")
UserPath = "C:\Users\" & Username 
strFolder = "C:\Users\" & Username & "\Desktop\CMS Supervisor*.lnk}" 
if FSO.FileExists(strFile) = True then  FSO.DeleteFile DesktopPath & "\CMS Supervisor*.lnk"


'Set objShell = Nothing 

Open in new window


Again - doesn't work but no error messages.  I'm going to try using the exact file name and see if the wildcards are a problem but they haven't been in the past.
I believe this code should work but you noted I'm using DesktopPath but didn't set it..right Mike?

How would I declare it?  Below is my guess but it isn't working. - no errors though.  I did test to make sure that the username is getting pulled correctly using WScript.echo - at least I know that much is working.

Dim FSO, WSHShell, Regkey, Username, strFile, DesktopPath, objShell

Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 
WScript.echo Username

Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = "C:\Users\" & Username & "Desktop"
strFile = "C:\Users\" & Username & "\Desktop\CMS Supervisor*.lnk" 
if FSO.FileExists(strFile) = True then  FSO.DeleteFile DesktopPath & "\CMS Supervisor*.lnk"

Open in new window

@ Ken

As I mentioned in my post and again here since you're code shows it again....

strFile contains a wildcard. FileExists doesn't support wildcards.
DeleteFile supports wildcards.
Thanks NVIT. I'm making headway on the VBScript.  The code below works but I have to use "On error" or an error messages comes up if the file doesn't exist.  At this point I'm having trouble getting the "if exists" part working.  Now that you reminded me that strFile doesn't support wildcards I need to figure that part out.  What can I use in VB to check if a file exists using wildcard?

Dim FSO, WSHShell, Regkey, Username, strFile, DesktopPath, objShell

Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 
WScript.echo Username

Set Shell = CreateObject("WScript.Shell") 
Set FSO = CreateObject("Scripting.FileSystemObject") 
DesktopPath = "C:\Users\" & Username & "\Desktop"
If Dir("C:\Users\" & Username & "\Desktop\CMS Supervisor*.lnk") <> vbNullString Then FSO.DeleteFile DesktopPath & "\CMS Supervisor*.lnk"

Open in new window

> What can I use in VB to check if a file exists using wildcard

IIRC, there is none built-in. You'd need some kind of loop like this...

Dim FSO, WSHShell, Regkey, Username, strFolder, UserPath, objShell
Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 

Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = "C:\Users\" & Username & "\Desktop" 

objStartFolder = DesktopPath & "\"

Set objFolder = FSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
   if instr(objFile.Name,"CMS Supervisor") <> 0 AND instr(objFile.Name,".lnk") <> 0 then
       msgbox "Put DeleteFile here " & objFile.Name
   end if
Next

Open in new window

If anyone can help me with this last error it would be great.  So the code below works but only with "On error resume next".  As long as I have that the file get's deleted.  When I comment out the On error I get the following error message..

User generated image

On error resume next
Dim FSO, WSHShell, Regkey, Username, strFile, DesktopPath, objShell

Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 
WScript.echo Username

Set Shell = CreateObject("WScript.Shell") 
Set FSO = CreateObject("Scripting.FileSystemObject") 
DesktopPath = "C:\Users\" & Username & "\Desktop"
If Dir("C:\Users\" & Username & "\Desktop\CMS Supervisor*.lnk") <> vbNullString Then FSO.DeleteFile DesktopPath & "\CMS Supervisor*.lnk"

Open in new window


Looking up the error code doesn't seem to help much with that last line.
I get the same error as you. For some reason, DIR doesn't work in VBS.
Just use the loop I posted
I updated the code with the loop.  It fails on line 11 with "Object required" 800A01A8.  Did I miss/typo something?

' On error resume next
Dim FSO, WSHShell, Regkey, Username, strFile, strFolder, UserPath, DesktopPath, objShell

Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 
WScript.echo Username

objStartFolder = U & "\"

Set objFolder = FSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
   if instr(objFile.Name,"CMS Supervisor") <> 0 AND instr(objFile.Name,".lnk") <> 0 then
       msgbox "Put DeleteFile here " & objFile.Name
   end if
Next

Open in new window

Line 11....sorry.
Line 9?
Also, what am I supposed to put in the msgbox line that says this?

"Put DeleteFile here "
@NVIT - line 10.  Sorry for the confusion.  It's getting the Object required error on this line.

objStartFolder = U & "\"
What is U? Should it be Username?
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
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