Scripting Question

Deleting all Links to a file in a folder

01/06/2016 690 views
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"
        Remove-Item $userpath\* -Recurse -ErrorVariable errs -ErrorAction SilentlyContinue  
    catch {
        "$errs" | Out-File c:\temp\allegroerror.txt -append
0 Comments   [ + ] Show comments


All Answers

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
Answered 01/07/2016 by: python
Senior White Belt

This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ