用于删除每个userprofile下的文件的脚本

时间:2017-03-20 14:20:52

标签: powershell user-profile

我正在寻找一个脚本来运行以从每个userprofile + path ex中删除文件:userprofile \ Appdata \ Microsoft \ Windows \ WER \ ReportQueue *

我试过

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

不行。

还试过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

2 个答案:

答案 0 :(得分: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

    }
}

答案 1 :(得分:0)

您可以将Get-ChildItem-Directory参数一起使用以获取C:\ Users中的子目录,然后将其路径($_.FullName)与您想要的子路径相连接。< / p>

然后使用Test-PathRemove-Item删除您想要的文件。

$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
    }
}