SOLVED

Intune installed desktop shortcut needs to be removed

Brass Contributor

created a desktop shortcut via intune windows app (win 32). Here is my script

 

New-Item -Path "c:" -Name "mem" -ItemType "directory" -Force

Copy-Item "S:\Shortcuts\UKG.ico" -Destination "c:\mem\UKG.ico"

 

$Shell = New-Object -ComObject ("WScript.Shell")

$ShortCut = $Shell.CreateShortcut("C:\users\public\desktop\UKG.lnk")

$ShortCut.TargetPath="-------------------------------------"

$Shortcut.Arguments="------------------------------------"

$ShortCut.IconLocation = "c:\mem\UKG.ico";

$ShortCut.Description = "UKG Shortcut";

$ShortCut.Save()

 

(I replaced the path with ————————)

 

The app works fine and copies the ico file from a network share and places it on the c drive in a folder it creates named mem. Now after creating the shortcut they have decided to use SSO which is a new address. I need to delete the ico file in the mem folder and remove the desktop shortcut. I created a simple script.

 

$ShortcutFile = "$env:Public\Desktop\UKG.lnk"

if (Test-Path $ShortcutFile){ Remove-Item $ShortcutFile }

else { Write-Output "Shortcut Not Found" }

 

To just delete the shortcut. When I run the script as an admin in powershell it works just fine. If I try and run the script in a normal powershell it fails and says it doesn't have access to the public desktop. I ran the file in Intune as just a script and it fails.  I converted it to an intunewin file and added it as an intune windows app (win 32). It successfuly runs on all my pcs but does not remove the icon. I'm at a loss and I really need to remove this icon so I can push the new one. Any suggestions on how to remove it via a script or app? I've checked into remediation scripts but that isn't going to be an option for now.

 

 

 

23 Replies

@Jesse13579 Running it as a Win32 app is perfect, but you should run it as a System and not as a User because the User doesn't have permissions in the c:\users\public folder.

 

Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.

If one of the posts was helpful in other ways, please consider giving it a Like.

I’m new with intune. How do I go about running it as a system?

@Jesse13579 When you add an Intunewin app to Intune, you can select the Installation behavior to be System or User.

 

Harm_Veenstra_0-1704891070079.png

 

@Harm_Veenstra I just checked and I do have the instillation behavior set to System.  I went through and verified a few of the machines and the desktop shortcut is still there even though the app says it ran correctly.  

$ShortcutFile = "$env:Public\Desktop\UKG.lnk"

if (Test-Path $ShortcutFile){ Remove-Item $ShortcutFile -Force:True -Confirm:$False}

else { Write-Output "Shortcut Not Found" }

Does that work? Test it on your machine first in a powershell prompt

@Harm_Veenstra when I run that script in an elevated powershell or a regular powershell I get

 

 

PS C:\ukg2> .\uninstall.ps1
Remove-Item : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by
parameter 'Force'.
At C:\ukg2\uninstall.ps1:3 char:64
+ ... st-Path $ShortcutFile){ Remove-Item $ShortcutFile -Force:True -Confir ...
+ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.RemoveItemCommand

 

 

When I run the original script 

 

$ShortcutFile = "$env:Public\Desktop\UKG.lnk"

if (Test-Path $ShortcutFile){ Remove-Item $ShortcutFile }

else { Write-Output "Shortcut Not Found" }

 

 

I get this error in a standard powershell window.  

 

Remove-Item : Cannot remove item C:\Users\Public\Desktop\UKG.lnk: Access to the path 'C:\Users\Public\Desktop\UKG.lnk'
is denied.
At C:\ukg2\uninstall2.ps1:3 char:31
+ if (Test-Path $ShortcutFile){ Remove-Item $ShortcutFile }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Users\Public\Desktop\UKG.lnk:FileInfo) [Remove-Item], Unauthorized
AccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

My bad! if (Test-Path $ShortcutFile){ Remove-Item $ShortcutFile -Force:True -Confirm:$False} should be if (Test-Path $ShortcutFile){ Remove-Item $ShortcutFile -Force:$True -Confirm:$False}

I didn't type the required $ in the -Force parameter value :(
I still get

Remove-Item : Cannot remove item C:\Users\Public\Desktop\UKG.lnk: Access to the path is denied.
At C:\ukg2\uninstall.ps1:3 char:31
+ ... hortcutFile){ Remove-Item $ShortcutFile -Force:$True -Confirm:$False}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Users\Public\Desktop\UKG.lnk:FileInfo) [Remove-Item], ArgumentExcep
tion
+ FullyQualifiedErrorId : RemoveFileSystemItemArgumentError,Microsoft.PowerShell.Commands.RemoveItemCommand


In a nonelevated powershell. In an elevated powershell it works and removes the icon.
That is correct, and you can't remove things from the folder as a regular user. You can do it as Admin, and in Intune, you can when you assign it as System for the install behavior.
I just set the app back up and used the new script. I assigned it to system like before. I've got it running and I'll let you know. I greatly appreciate the help.
No problem, let us know how it works out!
It ran on my 3 test machines and I verified that it did not remove the shortcut from those 3 machines. The app status is installed. It just didn't remove the desktop shortcuts.
Ok, then there could be something wrong with the detection, perhaps... What command line do you use for installing and uninstalling? What do you use for the detection? Did you assign it to users to be installed or uninstalled? (Group Assignment)
For my requirements I set it for 32 and 64 bit and the min operating system is windows 10 1607. My detection rule is manually configured and file rule type. The path is c:\users\public\desktop and the file or folder is ukg.lnk. The detection method is file or folder exists. It could be my install and uninstall command. I've tried a few things for those but now that I think about it that could be the problem. Do you have any suggestions on how to set those up?
best response confirmed by Jesse13579 (Brass Contributor)
Solution
The detection is OK if you want the shortcut removed. You must assign a group to Uninstall the program, and it will run the uninstall script. The detection will check again to see if the file is still there. If not, The removal of the shortcut should be complete.

What does your uninstall command line look like? It should be powershell.exe -Executionpolicy Bypass -File .\uninstall.ps1
That might be my problem. My uninstall was Powershell.exe -Executionpolicy Bypass .\uninstall.ps1. I'll make the change and see what happens.
That should also work, -File is just for clarity. Ok... Then the assignment part, did you assign a group to uninstall the software?
I have my all endpoints group assigned to uninstall. I installed it to that group to begin with.
Could you add this to the top of the script: Start-Transcript c:\windows\temp\log.txt ? And this at the end: Stop-Transcript. Create a new .intunewin file again, deploy it, watch the status, and check c:\windows\temp\log.txt afterward...
1 best response

Accepted Solutions
best response confirmed by Jesse13579 (Brass Contributor)
Solution
The detection is OK if you want the shortcut removed. You must assign a group to Uninstall the program, and it will run the uninstall script. The detection will check again to see if the file is still there. If not, The removal of the shortcut should be complete.

What does your uninstall command line look like? It should be powershell.exe -Executionpolicy Bypass -File .\uninstall.ps1

View solution in original post