← script library

Managing File and Folder Permissions with PowerShell

Security & AuditingJanuary 4, 2025

In this post, we will focus on managing file and folder permissions using PowerShell. Properly managing permissions is critical for ensuring data security and defined access control within your organization. This script will help you efficiently retrieve and manage permissions for specific folders and files, allowing you to quickly identify who has access to what.

powershell
# Define the target directory
$targetDirectory = "C:\Path\To\Your\Folder"  # Change this to your target folder path
# Retrieve the ACL for the directory
$acl = Get-Acl -Path $targetDirectory
$acl | Format-List

# Display permissions for all users
$acl.Access | ForEach-Object {
    Write-Host "Identity: $($_.IdentityReference) - Permission: $($_.FileSystemRights) - Type: $($_.AccessControlType)"
}

# Specify the user to remove and the permission to remove
$userToRemove = "DOMAIN\User"  # Change to the user you wish to remove
$accessRule = $acl.Access | Where-Object { $_.IdentityReference -eq $userToRemove }
if ($accessRule) {
    $acl.RemoveAccessRule($accessRule)
    Set-Acl -Path $targetDirectory -AclObject $acl
    Write-Host "Removed permissions for user: $userToRemove"
} else {
    Write-Host "User $userToRemove does not have any permissions on this folder."
}

# Specify the user to add and the permissions to grant
$userToAdd = "DOMAIN\NewUser"  # Change to the user you wish to add
$permission = "ReadAndExecute"   # Define permission type
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($userToAdd, $permission, "Allow")
$acl.AddAccessRule($accessRule)
Set-Acl -Path $targetDirectory -AclObject $acl
Write-Host "Granted $permission permissions to user: $userToAdd"

# Re-confirm the ACL after modifications
$updatedAcl = Get-Acl -Path $targetDirectory
Write-Host "Updated permissions for $targetDirectory:"
$updatedAcl.Access | ForEach-Object {
    Write-Host "Identity: $($_.IdentityReference) - Permission: $($_.FileSystemRights) - Type: $($_.AccessControlType)"
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.