-3

I am looking for a script to run to remove files from each userprofile + path ex: userprofile\Appdata\Microsoft\Windows\WER\ReportQueue*

I tried

Remove-Item "C:\users + \AppData\Local\Microsoft\Windows\WER\ReportQueue\AppCrash*"

No go.

Also tried VBScript:

Set fso = CreateObject("Scripting.FileSystemObject")

strOneDrivePath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERPROFILE%") & "\AppData\Local\Microsoft\Windows\WER\ReportQueue\"
Search strOneDrivePath

Sub Search(str)
    Set folder = fso.GetFolder(str)
    For Each file In folder.Files
        If file.DateLastModified < (Now() - 3) Then
            file.Delete True
        End If
    Next

    For Each subFolder In folder.SubFolders
        Search subFolder.Path 
        If subFolder.Files.Count = 0  Then
            subFolder.Delete True
        End If
    Next
End Sub
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
RankSolo
  • 1
  • 1
  • 1
  • 2
  • Welcome to Stack Overflow! We are a community to help programmers and programming enthusiasts. That being said, it is expect that you show what you have done or tried before posting. This gives us something to build on. As of now this reads like a code writing request which is off topic for SO. Break your question into its parts and search individually for solutions to those problems. Then, if you are still having issues, please [edit] your question showing your work so we can better help you and the community. – Matt Mar 20 '17 at 14:22
  • The command you tried looks for a path that is literally `C:\users + \AppData\Local\Mic...`. You need to enumerate the subfolders of `C:\Users` and join the rest of the path to those base paths. – Ansgar Wiechers Mar 20 '17 at 14:27

2 Answers2

0
$paths = Get-ChildItem -Directory c:\users | Select-Object $_.Name

ForEach ($path in $paths){
    If (test-path "c:\users\$path\AppData\Local\Microsoft\Windows\WER\ReportQueue\AppCrash")
    {
        Remove-Item -Path "c:\users\$path\AppData\Local\Microsoft\Windows\WER\ReportQueue\AppCrash\*" -Force

    }
}
jigfox
  • 18,057
  • 3
  • 60
  • 73
  • Can you explain your answer instead of just posting code? Please also format the code (indent 4 spaces or use the `{}` button). – Robert Mar 20 '17 at 15:42
0

You can use Get-ChildItem with the -Directory param to get the sub-directories within C:\Users, then join their paths ($_.FullName) with the child path you want.

Then use Test-Path and Remove-Item to delete the files you wish.

$path = "C:\Users"
$child_path = "AppData\Local\Microsoft\Windows\WER\ReportQueue"
$files_filter = "AppCrash*"

Get-ChildItem $path -Directory -Exclude Default*,Public | foreach {

    $joined_path = Join-Path -Path $_.FullName -ChildPath $child_path
    If (test-path $joined_path) {
        Remove-Item "$joined_path\$files_filter" -Force
    }
}
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40