We have a lot of users with duplicate files on their Desktop (Windows 10). I want to create a script which runs every X minutes or just once after logging on to the system.
Our users are currently seeing the following shortcuts (.lnk):
So in short I want to keep the 'original' shortcuts but I want to remove all shortcuts containing '- copy.lnk' and/or '-computername.lnk'.
I also tried using the %computername% variable but I have no idea how to implement this in my code. Every computer has a different and unique name so I can not use pre-defined computernames.
Any tips on how I can achieve this? I have tried using the code below, but this removes all .lnk (shortcut) files and not just the duplicates.
#(Get-ChildItem Dummy.lnk | Select-String -Pattern "OFFICE" | Select-Object -ExpandProperty path -Unique) | ForEach-Object{Remove-Item -Force -LiteralPath $_}
I prefer using Powershell so that I can deploy the script using Group Policy.
I would use the Where-Object cmdlet to filter all shortcuts you want to remove and pipe them to the Remove-Item cmdlet:
Get-ChildItem -Path 'c:\yourPath' -Filter '*.lnk' |
Where-Object { $_.Name -match '- Copy\.lnk$' -or $_.Name -like "*$($env:Computername)*"} |
Remove-Item
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With