Powershell 获取Childitem目录通配符";“访问被拒绝”;

Powershell 获取Childitem目录通配符";“访问被拒绝”;,powershell,directory,permissions,cmdlet,Powershell,Directory,Permissions,Cmdlet,我有一个脚本,我递归地删除某些目录中的一些文件。问题在于该目录的规范 当我尝试显式指定它时(即仅清除user1 dir),它可以正常工作: $temp = "C:\Users\user1\AppData\Local\Temp\" Get-ChildItem $temp -Recurse -Force -Verbose -ErrorAction SilentlyContinue | remove-item -force -Verbose -recurse -ErrorVariable Failed

我有一个脚本,我递归地删除某些目录中的一些文件。问题在于该目录的规范

当我尝试显式指定它时(即仅清除user1 dir),它可以正常工作:

$temp = "C:\Users\user1\AppData\Local\Temp\"
Get-ChildItem $temp -Recurse -Force -Verbose -ErrorAction SilentlyContinue | remove-item -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue
但是,当我使用通配符指定它时(即影响这台电脑上的所有用户)

它因错误而失败

Get-ChildItem : Access is denied
At line:7 char:1
+ Get-ChildItem $localTempDir -Recurse -Force -Verbose -ErrorAction Sil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand
怎么会这样? 这绝对不是权限问题,因为它是相同的。 是的,我以提升的权限运行脚本。
该格式中规定的其他目录,例如

C:\Users\*\AppData\Local\Microsoft\Windows\Caches
C:\Windows\Temp\*

像一个符咒一样被清除。

如果您想排除一个配置文件列表,并处理一个子文件夹列表,也许您可以使用以下方法:

$targets = "AppData\Local\Microsoft\Windows\Caches",
           "AppData\Local\Microsoft\Windows\Temporary Internet Files"

$special = @("Default", "Public")

$profiles = Get-ChildItem "C:\Users" -Directory |
    Where-Object Name -NotIn $special |
    Select-Object -ExpandProperty FullName

$profiles | ForEach-Object {
    foreach($target in $targets) {
        $path = Join-Path $_ $target
        #delete/empty $path
    }
}

注意:语法是PS3.0+

Hi,这难道不能与特殊/隐藏/系统配置文件文件夹相关吗?e、 g.
默认值
可能。如何诊断此问题?枚举配置文件,排除特殊配置文件,应用其余逻辑?嗯,它可以正常工作,但是如果我从
$special
中删除
Public
,脚本也会影响它。但是如果我删除
默认值
,它的垃圾也不会被删除。为什么?脚本是否只处理非隐藏(非系统)目录?默认文件夹也可以包含许多临时文件,因此应该进行处理。我认为
-Force
可以帮助您进行
gci
更正!工作完全按照预期进行。但是,我仍然不清楚我的通配符表单
C:\Users\*\AppData\Local\Temp\*
有什么问题。为什么
gci
在这里失败?
$targets = "AppData\Local\Microsoft\Windows\Caches",
           "AppData\Local\Microsoft\Windows\Temporary Internet Files"

$special = @("Default", "Public")

$profiles = Get-ChildItem "C:\Users" -Directory |
    Where-Object Name -NotIn $special |
    Select-Object -ExpandProperty FullName

$profiles | ForEach-Object {
    foreach($target in $targets) {
        $path = Join-Path $_ $target
        #delete/empty $path
    }
}