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

Comments

Please log in to comment

Answers

1
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

Please log in to comment
Answer this question or Comment on this question for clarity
Admin Script Editor
Admin Script Editor is an integrated scripting environment available free here at ITNinja

Share