You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell列出Windows文件夹权限时如何排除指定内置用户

问题原因

你将-notmatch比较运算直接写在了「组/用户」字段的赋值语句中,比较运算的返回结果为布尔值(True/False),因此该字段会直接显示判断结果而非用户名本身。

修正方案

将过滤逻辑移动到循环判断中,匹配到排除列表内的用户/组时直接跳过当前条目,不加入输出集合即可。

修正后完整脚本
$ignore = @('BUILTIN\Administrators','CREATOR OWNER')
$ExcludeUsersRegex = ($ignore | ForEach-Object { [regex]::Escape($_) }) -join '|'

$FolderPath = Get-ChildItem -Directory -Path "D:\MSSQL" -Recurse -Force
$Output = @()
ForEach ($Folder in $FolderPath) {
    $Acl = Get-Acl -Path $Folder.FullName
    ForEach ($Access in $Acl.Access) {
        # 新增过滤判断:匹配到排除列表的用户直接跳过
        if ($Access.IdentityReference -match $ExcludeUsersRegex) {
            continue
        }
        $Properties = [ordered]@{
            'Folder Name' = $Folder.FullName
            'Group/User' = $Access.IdentityReference
            'Permissions' = $Access.FileSystemRights
            'Inherited' = $Access.IsInherited
        }
        $Output += New-Object -TypeName PSObject -Property $Properties            
    }
}
$Output | Out-GridView
修改点说明
  • 新增了前置判断逻辑:遍历每个权限条目时先校验用户是否在排除列表内,匹配成功则直接跳过当前条目处理
  • 还原了「组/用户」字段的赋值逻辑,直接写入用户名本身,不再嵌套比较运算

内容的提问来源于stack exchange,提问作者MISNole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 20:06:03