Skip to content

Instantly share code, notes, and snippets.

@emmaly
Created January 15, 2023 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmaly/ff50aa8d063f356cf0dfb144798ee16c to your computer and use it in GitHub Desktop.
Save emmaly/ff50aa8d063f356cf0dfb144798ee16c to your computer and use it in GitHub Desktop.
Search-FileAccess PowerShell Script
function Search-FileAccess {
param (
[Parameter(Mandatory=$true)]
[string]$IdentityReferenceRegex,
[Parameter(Mandatory=$true)]
[string]$Path,
[Boolean]$Recurse=$true,
[switch]$Visualization
)
$matchingFolders = Get-ChildItem -Recurse:$Recurse $Path | Where-Object {
$isDirectory = $_.Attributes -contains 'Directory'
$accessList = $_ | Get-Acl | Select-Object -ExpandProperty Access
$matchingAccessList = $accessList | Where-Object {
$_.IdentityReference.Value -match $IdentityReferenceRegex
}
$accessListPersonMatched = $matchingAccessList.Count -gt 0
$matchingAccessList = $matchingAccessList | Where-Object IsInherited -eq $false
$fileMatched = $matchingAccessList.Count -gt 0
if ($Visualization) {
$symbol = if ($isDirectory) { '#' } else { ':' }
$color = if ($fileMatched) { 'Red' } else {
if ($accessListPersonMatched) { 'DarkGreen' } else { 'Green' }
}
Write-Host $symbol -ForegroundColor $color -NoNewline
}
return $fileMatched
}
if ($Visualization) {
Write-Host "`n`n`n"
}
if ($VerbosePreference) {
$statusColor = if ($matchingFolders.Count -gt 0) { 'Red' } else { 'Cyan'}
Write-Host "`n`nMatching folders: $($matchingFolders.Count)" -ForegroundColor $statusColor
$matchingFolders | Select-Object -ExpandProperty FullName | Sort-Object | Out-Host
}
return $matchingFolders
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment