清空目标文件夹下所有递归文件和文件夹的审核者,同时保留权限:
# 定义目标文件夹路径(可根据需要修改)
$targetPath = "C:\"
# 清空目标文件夹本身的审核者(保留权限,跳过无效规则)
$acl = Get-Acl -Path $targetPath
$newAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $acl.Access) {
try {
$newAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference)"
}
}
Set-Acl -Path $targetPath -AclObject $newAcl
Write-Host "已处理根目录: $targetPath" -ForegroundColor Green
# 清空目标文件夹下所有递归文件和文件夹的审核者(保留权限,跳过无效规则),并显示进度
$items = Get-ChildItem -Path $targetPath -Recurse -ErrorAction SilentlyContinue
$total = $items.Count
$current = 0
Write-Host "总共发现 $($total) 个项,开始处理..." -ForegroundColor Yellow
foreach ($item in $items) {
$current++
# 显示进度条
$percent = if ($total -gt 0) { ($current / $total) * 100 } else { 0 }
Write-Progress -Activity "清空审核者(保留权限)" -Status "处理: $($item.FullName)" -PercentComplete $percent -CurrentOperation "进度: $current / $total"
try {
$currentAcl = Get-Acl -Path $item.FullName
$currentNewAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $currentAcl.Access) {
try {
$currentNewAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference) 在路径 $($item.FullName)"
}
}
Set-Acl -Path $item.FullName -AclObject $currentNewAcl
}
catch {
Write-Error "无法处理路径 $($item.FullName): $($_.Exception.Message)"
}
}
# 完成进度显示
Write-Progress -Activity "清空审核者(保留权限)" -Completed
Write-Host "所有项处理完成!总计: $total 个。" -ForegroundColor Green
清空指定目录及其所有递归文件和文件夹的审核者设置,同时保留权限。并排除指定的目录列表及其所有子内容(包括子文件夹和文件):
# 定义目标文件夹路径(可根据需要修改,例如 "C:\Windows")
$targetPath = "C:\"
# 定义排除目录列表(完整路径,用双引号包围;可添加/修改)
$excludeDirs = @(
"C:\Program Files\WindowsApps",
"C:\Program Files\Windows Defender",
"C:\Program Files\Windows Defender Advanced Threat Protection",
"C:\Program Files\ModifiableWindowsApps",
"C:\Program Files (x86)\InstallShield Installation Information",
"C:\Program Files (x86)\Microsoft",
"C:\Program Files (x86)\Windows Defender",
"C:\ProgramData",
"C:\Windows",
"C:\Users"
)
# 清空目标文件夹本身的审核者(保留权限,跳过无效规则)
$acl = Get-Acl -Path $targetPath
$newAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $acl.Access) {
try {
$newAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference)"
}
}
Set-Acl -Path $targetPath -AclObject $newAcl
Write-Host "已处理根目录: $targetPath" -ForegroundColor Green
# 清空目标文件夹下所有递归文件和文件夹的审核者(保留权限,跳过无效规则),并排除指定目录,显示进度
$allItems = Get-ChildItem -Path $targetPath -Recurse -ErrorAction SilentlyContinue
# 过滤排除目录及其子内容
$items = $allItems | Where-Object {
$excludeMatch = $false
foreach ($exclude in $excludeDirs) {
if ($_.FullName -like "$exclude*" -or $_.FullName -eq $exclude) {
$excludeMatch = $true
break
}
}
-not $excludeMatch
}
$total = $items.Count
$current = 0
Write-Host "总共发现 $($total) 个项(已排除指定目录),开始处理..." -ForegroundColor Yellow
foreach ($item in $items) {
$current++
# 显示进度条
$percent = if ($total -gt 0) { ($current / $total) * 100 } else { 0 }
Write-Progress -Activity "清空审核者(保留权限)" -Status "处理: $($item.FullName)" -PercentComplete $percent -CurrentOperation "进度: $current / $total"
try {
$currentAcl = Get-Acl -Path $item.FullName
$currentNewAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $currentAcl.Access) {
try {
$currentNewAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference) 在路径 $($item.FullName)"
}
}
Set-Acl -Path $item.FullName -AclObject $currentNewAcl
}
catch {
Write-Error "无法处理路径 $($item.FullName): $($_.Exception.Message)"
}
}
# 完成进度显示
Write-Progress -Activity "清空审核者(保留权限)" -Completed
Write-Host "所有项处理完成!总计: $total 个。" -ForegroundColor Green
清空指定目录及目录中所有文件的审核者设置,同时保留权限。不清空目录下的子目录(及子目录的所有内容):
# 定义目标文件夹路径(可根据需要修改,例如 "C:\Windows")
$targetPath = "C:\"
# 清空目标文件夹本身的审核者(保留权限,跳过无效规则)
$acl = Get-Acl -Path $targetPath
$newAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $acl.Access) {
try {
$newAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference)"
}
}
Set-Acl -Path $targetPath -AclObject $newAcl
Write-Host "已处理根目录: $targetPath" -ForegroundColor Green
# 清空目标文件夹下直接文件(不包括子文件夹)的审核者(保留权限,跳过无效规则)
# 注意:通过 Get-ChildItem -File 自动排除所有子目录及其内容,无需手动排除列表
$files = Get-ChildItem -Path $targetPath -File -ErrorAction SilentlyContinue
$total = $files.Count
$current = 0
Write-Host "总共发现 $($total) 个直接文件(自动排除所有子目录),开始处理..." -ForegroundColor Yellow
foreach ($file in $files) {
$current++
# 显示进度条
$percent = if ($total -gt 0) { ($current / $total) * 100 } else { 0 }
Write-Progress -Activity "清空审核者(保留权限)" -Status "处理文件: $($file.FullName)" -PercentComplete $percent -CurrentOperation "进度: $current / $total"
try {
$currentAcl = Get-Acl -Path $file.FullName
$currentNewAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $currentAcl.Access) {
try {
$currentNewAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference) 在文件 $($file.FullName)"
}
}
Set-Acl -Path $file.FullName -AclObject $currentNewAcl
}
catch {
Write-Error "无法处理文件 $($file.FullName): $($_.Exception.Message)"
}
}
# 完成进度显示
Write-Progress -Activity "清空审核者(保留权限)" -Completed
Write-Host "所有直接文件处理完成!总计: $total 个。所有子目录及其内容未处理。" -ForegroundColor Green
清空指定目录及所有递归项(子文件夹及其内容),但自动排除根目录下的直接文件:
# 定义目标文件夹路径(可根据需要修改,例如 "C:\Windows")
$targetPath = "C:\"
# 清空目标文件夹本身的审核者(保留权限,跳过无效规则)
$acl = Get-Acl -Path $targetPath
$newAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $acl.Access) {
try {
$newAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference)"
}
}
Set-Acl -Path $targetPath -AclObject $newAcl
Write-Host "已处理根目录: $targetPath" -ForegroundColor Green
# 清空目标文件夹下递归内容(子文件夹及其所有内容)的审核者(保留权限,跳过无效规则)
# 注意:通过过滤自动排除根目录下的直接文件,只处理递归项
$allItems = Get-ChildItem -Path $targetPath -Recurse -ErrorAction SilentlyContinue
$items = $allItems | Where-Object { -not ($_.PSIsContainer -eq $false -and $_.DirectoryName -eq $targetPath) }
$total = $items.Count
$current = 0
Write-Host "总共发现 $($total) 个递归项(自动排除根直接文件),开始处理..." -ForegroundColor Yellow
foreach ($item in $items) {
$current++
# 显示进度条
$percent = if ($total -gt 0) { ($current / $total) * 100 } else { 0 }
Write-Progress -Activity "清空审核者(保留权限)" -Status "处理: $($item.FullName)" -PercentComplete $percent -CurrentOperation "进度: $current / $total"
try {
$currentAcl = Get-Acl -Path $item.FullName
$currentNewAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $currentAcl.Access) {
try {
$currentNewAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference) 在路径 $($item.FullName)"
}
}
Set-Acl -Path $item.FullName -AclObject $currentNewAcl
}
catch {
Write-Error "无法处理路径 $($item.FullName): $($_.Exception.Message)"
}
}
# 完成进度显示
Write-Progress -Activity "清空审核者(保留权限)" -Completed
Write-Host "所有递归内容处理完成!总计: $total 个。根直接文件未处理。" -ForegroundColor Green
清空指定目录及所有递归项(子文件夹及其内容),可以设定排除任意的子目录或者任意文件。如果排除路径不存在,命令忽略:
# 定义目标文件夹路径(可根据需要修改,例如 "C:\Windows")
$targetPath = "C:\"
# 定义排除路径列表(完整路径,用双引号包围;支持任意子目录或文件;目录会排除其所有子内容)
$excludePaths = @(
"C:\subfolder1", # 示例:排除子目录及其所有内容
"C:\another\file.txt", # 示例:排除特定文件
"C:\folder2\subfile.exe" # 示例:排除深层文件
)
# 清空目标文件夹本身的审核者(保留权限,跳过无效规则)
$acl = Get-Acl -Path $targetPath
$newAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $acl.Access) {
try {
$newAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference)"
}
}
Set-Acl -Path $targetPath -AclObject $newAcl
Write-Host "已处理根目录: $targetPath" -ForegroundColor Green
# 清空目标文件夹下所有递归内容(文件和子文件夹)的审核者(保留权限,跳过无效规则)
# 注意:排除指定的路径
$allItems = Get-ChildItem -Path $targetPath -Recurse -ErrorAction SilentlyContinue
# 过滤排除路径(目录排除子树,文件排除自身)
$items = $allItems | Where-Object {
$excludeMatch = $false
foreach ($exclude in $excludePaths) {
if ($_.FullName -like "$exclude*") {
$excludeMatch = $true
break
}
}
-not $excludeMatch
}
$total = $items.Count
$current = 0
Write-Host "总共发现 $($total) 个递归项(已排除指定路径),开始处理..." -ForegroundColor Yellow
foreach ($item in $items) {
$current++
# 显示进度条
$percent = if ($total -gt 0) { ($current / $total) * 100 } else { 0 }
Write-Progress -Activity "清空审核者(保留权限)" -Status "处理: $($item.FullName)" -PercentComplete $percent -CurrentOperation "进度: $current / $total"
try {
$currentAcl = Get-Acl -Path $item.FullName
$currentNewAcl = New-Object System.Security.AccessControl.DirectorySecurity
foreach ($access in $currentAcl.Access) {
try {
$currentNewAcl.AddAccessRule($access)
}
catch {
# 跳过无效的身份规则,继续处理其他规则
Write-Warning "跳过无效访问规则: $($access.IdentityReference) 在路径 $($item.FullName)"
}
}
Set-Acl -Path $item.FullName -AclObject $currentNewAcl
}
catch {
Write-Error "无法处理路径 $($item.FullName): $($_.Exception.Message)"
}
}
# 完成进度显示
Write-Progress -Activity "清空审核者(保留权限)" -Completed
Write-Host "所有递归内容处理完成!总计: $total 个。指定路径未处理。" -ForegroundColor Green
实际应用:
先将powershell升级到最新版本。
下载地址: https://github.com/PowerShell/PowerShell/releases
安装并重启 PowerShell,运行 pwsh 启动新版。
运行 $PSVersionTable 查看版本。如果powershell的版本不是7+,则进入文件夹C:\Program Files\PowerShell\7\ ,用管理员身份运行pwsh.exe即可进入7+版本powershell。
运行代码:
[[Requires]] -RunAsAdministrator # 确保以管理员运行
[[Requires]] -Version 7.0 # 要求 PowerShell 7+ 以支持 -Parallel
# 定义目标文件夹路径(建议从小目录测试,如 "D:\Test")
$targetPath = "C:\"
# 定义排除路径列表(完整路径,用双引号包围;支持任意子目录或文件;目录会排除其所有子内容)
# 增加更多常见排除以减少项数
$excludePaths = @(
"C:\Program Files\Windows Defender",
"C:\Program Files\Windows Defender Advanced Threat Protection",
"C:\Program Files\WindowsPowerShell",
"C:\Program Files (x86)\Microsoft",
"C:\Program Files (x86)\Windows Defender",
"C:\$Recycle.Bin", # 新增:回收站
"C:\System Volume Information", # 新增:系统卷信息
"C:\PerfLogs", # 新增:性能日志
"C:\pagefile.sys" # 新增:页面文件(文件级排除)
)
# 清空根目录的审核者(移除 SACL,保留 DACL)
try {
$acl = Get-Acl -Path $targetPath
# 启用审核继承(如果目录)
if ((Get-Item -Path $targetPath).PSIsContainer) {
$acl.SetAuditRuleProtection($false, $true) # 启用继承,复制现有规则
}
# 移除所有审核规则
$auditRules = $acl.GetAuditRules($true, $false, [System.Security.Principal.NTAccount])
foreach ($rule in $auditRules) {
$acl.RemoveAuditRule($rule)
}
Set-Acl -Path $targetPath -AclObject $acl
Write-Host "已处理根目录: $targetPath" -ForegroundColor Green
}
catch {
Write-Error "无法处理根目录 $targetPath : $($_.Exception.Message)"
}
# 清空目标文件夹下所有递归内容(文件和子文件夹)的审核者
# 先获取所有项,然后高效过滤排除
$allItems = Get-ChildItem -Path $targetPath -Recurse -ErrorAction SilentlyContinue
# 过滤排除路径(目录排除子树,文件排除自身) - 改回可靠的 foreach + -like 方式,确保准确跳过
$items = $allItems | Where-Object {
$excludeMatch = $false
foreach ($exclude in $excludePaths) {
if ($_.FullName -like "$exclude*") {
$excludeMatch = $true
break
}
}
-not $excludeMatch
}
$total = $items.Count
Write-Host "总共发现 $($total) 个递归项(已排除指定路径),开始并行处理..." -ForegroundColor Yellow
# 并行处理:使用 ForEach-Object -Parallel,限制并发数以避免 IO/权限冲突
$items | ForEach-Object -Parallel {
$item = $_
try {
$acl = Get-Acl -Path $item.FullName
# 启用审核继承(仅目录),以确保子项传播
if ($item.PSIsContainer) {
$acl.SetAuditRuleProtection($false, $true) # 启用继承,复制现有规则
}
# 显式移除所有审核规则(更彻底)
$auditRules = $acl.GetAuditRules($true, $false, [System.Security.Principal.NTAccount])
foreach ($rule in $auditRules) {
$acl.RemoveAuditRule($rule)
}
# 保留 DACL
Set-Acl -Path $item.FullName -AclObject $acl
}
catch {
Write-Warning "跳过或无法处理: $($item.FullName) - $($_.Exception.Message)"
}
} -ThrottleLimit 4 # 限制 4 个并发线程,防止过度负载系统(可根据机器调整到 8)
Write-Host "所有递归内容处理完成!总计: $total 个。指定路径未处理。" -ForegroundColor Green
PowerShell 代码删除文件的审计者,文件可以是一个列表。注意文件名可能包含特殊字符空格中文等等。
# 定义要清除审计规则的文件列表(支持空格、中文、特殊字符)
$fileList = @(
"C:\Users\你的用户名\Desktop\测试 文件.txt"
"D:\我的文档\特殊&字符(文件).docx"
"E:\中文路径\项目报告 2025.pdf"
"C:\Temp\file with spaces and 'quotes'.log"
"C:\Program Files\App\config.json"
# 在这里添加更多文件路径,每行一个,用双引号包围
)
# 批量处理每个文件:清除审计规则,保留权限规则
foreach ($filePath in $fileList) {
# 跳过空行
if (-not $filePath.Trim()) { continue }
# 检查文件是否存在(提前避免不必要的 ACL 操作)
if (-not (Test-Path -Path $filePath -PathType Leaf)) {
Write-Warning "文件不存在,跳过: $filePath"
continue
}
try {
# 尝试获取当前文件的 ACL
# 使用 -ErrorAction Stop 以便捕获访问拒绝等错误
$acl = Get-Acl -Path $filePath -ErrorAction Stop
# 创建一个全新的 FileSecurity 对象
$newAcl = New-Object System.Security.AccessControl.FileSecurity
# 只复制访问规则(权限 DACL),忽略审计规则(SACL)
foreach ($accessRule in $acl.Access) {
try {
$newAcl.AddAccessRule($accessRule)
}
catch {
Write-Warning "跳过无效的权限规则 ($($accessRule.IdentityReference)) 在文件: $filePath"
}
}
# 尝试设置新 ACL(清除审计规则)
Set-Acl -Path $filePath -AclObject $newAcl -ErrorAction Stop
Write-Host "成功清除审计规则: $filePath" -ForegroundColor Green
}
catch [System.UnauthorizedAccessException] {
# 专门捕获“拒绝访问”错误
Write-Warning "访问被拒绝,自动跳过文件: $filePath"
}
catch {
# 捕获其他所有错误(如路径无效、磁盘错误等)
Write-Warning "处理失败,自动跳过文件 ($filePath): $($_.Exception.Message)"
}
}
Write-Host "所有文件处理完成。" -ForegroundColor Cyan
列出系统全部驱动程序路径的命令:
Get-CimInstance Win32_SystemDriver |
Where-Object PathName -like "*\*.sys" |
ForEach-Object { '"' + $_.PathName + '"' } |
Out-File "$env:USERPROFILE\Desktop\drivers_paths_quoted.txt" -Encoding utf8
注意: 复制代码后,不要用右键粘贴到 powershell 中,用 ctrl+V 粘贴到 powershell 中。不然有可以出错!