Skip to content

Instantly share code, notes, and snippets.

@fdenzer
Created July 30, 2025 23:16
Show Gist options
  • Select an option

  • Save fdenzer/e5b5e7ee8b68aa9d1e7aa701e2f90de8 to your computer and use it in GitHub Desktop.

Select an option

Save fdenzer/e5b5e7ee8b68aa9d1e7aa701e2f90de8 to your computer and use it in GitHub Desktop.
param (
[Parameter(Mandatory = $false)]
[string]$TargetPath = (Read-Host "Enter the target path (e.g., F:\)")
)
# Resolve full path
$ResolvedPath = Resolve-Path -Path $TargetPath
$LogFile = Join-Path -Path (Get-Location) -ChildPath "PermissionLog.txt"
$denyRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
"Everyone", "Write, Modify", "ContainerInherit,ObjectInherit", "None", "Deny"
)
# Clear previous log (if any)
if (Test-Path $LogFile) {
Clear-Content -Path $LogFile
}
# Recursively process files and folders
Get-ChildItem -Path $ResolvedPath -Recurse -Force | ForEach-Object {
try {
$acl = Get-Acl $_.FullName
$acl.AddAccessRule($denyRule)
Set-Acl -Path $_.FullName -AclObject $acl
Add-Content -Path $LogFile -Value "SUCCESS: Denied Write/Modify on '$($_.FullName)'"
} catch {
Add-Content -Path $LogFile -Value "FAILURE: Could not set permissions on '$($_.FullName)' - $($_.Exception.Message)"
}
}
Write-Host "✅ Permission setting complete. Log saved to '$LogFile'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment