Share via


Delete shortcut in %USERPROFILE%\Desktop

Question

Monday, January 20, 2014 2:49 PM

Hi,

Does anyone know how to delete a shortcut "Shortcut1.lnk" from %USERPROFILE%\Desktop (not %ALLUSERSPROFILE%) and deploy via SCCM2012?

I have created a batch file with the following command line - del "%USERPROFILE%\Desktop\Shortcut1.lnk"

It works fine when manually running the batch file, although when running via a TS it errors (as the TS runs as system context) so wont work! I have also created a program with the command line with user context, but I cannot get this to work?

Can someone please advise how to run this as using user context?

Thanks

MCTS | MCITP | MCSA

All replies (14)

Monday, January 20, 2014 6:34 PM ✅Answered

Here's the revisited version:

@echo off
for /F "delims=" %%i in ('dir /b "%PUBLIC%\."') do del /Q /F "%systemdrive%\Users\%i\Desktop\file.lnk"

You could also do some checking (if exists) but don't think that's needed here, if it's not found, the script continues to next profile folder.


Tuesday, January 21, 2014 3:38 PM ✅Answered

Just to confirm, put this command-line into a package/program:

@echo off
for /F "delims=" %%i in ('dir /b "%PUBLIC%\."') do del /Q /F "%systemdrive%\Users\%i\Desktop\file.lnk"

You put this into a batch file and then put the batch file into a source location that is defined in a package. No program explicitly necessary.

How do call the command in the command line in the install properties, cmd /c ?

You use a Run command-line task sequence task that references the above package (which you've also distributed to your DPs). For the command-line, it's simply the name of the batch file you created above; e.g., myscript.bat.

Jason | http://blog.configmgrftw.com


Monday, January 20, 2014 3:29 PM

This batch file should work on Windows 7/8/8.1:

@echo off
for /F "delims=" %%i in ('dir /b "%PUBLIC%\."') do del /Q /F %systemdrive%\Users\%i\Desktop\file.lnk


Monday, January 20, 2014 3:54 PM

I now have the batch file working via a program using user context, do you know how to add this to a task sequence. When Im trying to add the package, no program is available to select?

Thanks

MCTS | MCITP | MCSA


Monday, January 20, 2014 4:04 PM

You'll have to use something like what narcoticoo posted above (which simply goes through each folder recursively) because TSes only run for the System and cannot be run in any user's context.

Jason | http://blog.configmgrftw.com


Monday, January 20, 2014 4:04 PM

You can add "Run commandline" and from there, Use Package.

For the command simply add: yourbatchfilename.bat

:)


Monday, January 20, 2014 4:12 PM

Yeah I have tried that, Im getting the below error:

*Software failed to run command line, hr=0x80070002. The operating system reported error 2147942402: The system cannot find the file specified. *

Thanks

MCTS | MCITP | MCSA


Monday, January 20, 2014 4:14 PM

Hi,

For all programs that should run in a Task Sequence, the following applies.

  • If not deployed select the "Allow these package to be deploy in a task sequence..."
  • Set to install "Whether or not a user is logged on"
  • "Allow users to interact..." can no be set

All application installations that run in a Task Sequence runs as a user, if you want to run it as a user I would suggest that you use the "Run command line" option and pass the credentials you want to use in that command instead.

Regards,
Jörgen

-- My System Center blog ccmexec.com -- Twitter @ccmexec


Monday, January 20, 2014 4:15 PM

Did you put the script in a package?

Are you using a Run command-line task?

Did you specify te pckage in the run command-line tsk?

What command-line are you using to invoke the batch file?

Jason | http://blog.configmgrftw.com


Monday, January 20, 2014 4:20 PM

Yes the script is in a package:

cmd.exe /c del "%USERPROFILE%\Desktop\Shortcut1.lnk"

Yes the run command line task has been used and the package specified.

Thanks Jason

MCTS | MCITP | MCSA


Monday, January 20, 2014 5:42 PM

Use my script instead.


Monday, January 20, 2014 6:16 PM

Use my script instead.

Exactly. As I mentioned above, the task sequence runs as local System only and so using %USERPROFILE% makes no sense. You need to enumerate through all available profile directories and delete the file in each -- narcoticoo's script does this (as mentioned).

Jason | http://blog.configmgrftw.com


Tuesday, January 21, 2014 8:07 AM

Thanks guys - sorry for the confusion. I will give narcoticoo's script a try!

MCTS | MCITP | MCSA


Tuesday, January 21, 2014 1:52 PM

Just to confirm, put this command-line into a package/program:

@echo off
for /F "delims=" %%i in ('dir /b "%PUBLIC%\."') do del /Q /F "%systemdrive%\Users\%i\Desktop\file.lnk"

How do call the command in the command line in the install properties, cmd /c ?

Can I use this program in a Task Sequence and do i need to run the program as user or system context?

Hope to hear from you soon!

Craig

MCTS | MCITP | MCSA