Deleting all Links to a file in a folder
I needed to delete the contents of a folder for every user this is located in C:\users. Used the following ps script. Now i need to delete any links that there might be to this folder or an exe file in this folder; cant seem to figure out how. Any help will be appreciated
-------------------------------------------------------------------------------------------------------
$users = Get-ChildItem c:\users | ?{ $_.PSIsContainer }
foreach ($user in $users){
$userpath = "C:\Users\$user\Gems"
Try{
Remove-Item $userpath\* -Recurse -ErrorVariable errs -ErrorAction SilentlyContinue
}
catch {
"$errs" | Out-File c:\temp\allegroerror.txt -append
}
}
-------------------------------------------------------------------------------------------------------
0 Comments
[ + ] Show comments
Answers (1)
Please log in to answer
Posted by:
python
8 years ago
Found a resolution online. Change Target as needed
----------------------------------------
function Get-StartMenuShortcuts{
$DesktopShortcuts = Get-ChildItem -ErrorAction SilentlyContinue -Recurse "C:\Users" -Include *.lnk
$StartMenuShortcuts = Get-ChildItem -ErrorAction SilentlyContinue -Recurse "C:\ProgramData\Microsoft\Windows\Start Menu" -Include *.lnk
$Shortcuts = $DesktopShortcuts + $StartMenuShortcuts
$Shell = New-Object -ComObject WScript.Shell
foreach ($Shortcut in $Shortcuts)
{
$Properties = @{
ShortcutName = $Shortcut.Name;
Path = $Shortcut.FullName;
ShortcutDirectory = $shortcut.DirectoryName
Target = $Shell.CreateShortcut($Shortcut).targetpath
}
New-Object PSObject -Property $Properties
}
[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
}
$ShortcutList = Get-StartMenuShortcuts
$ShortcutList | Where-Object{$_.Target -like "*Hello.exe"} | Remove-Item -ErrorAction SilentlyContinue