← script library

Efficient File and Folder Permissions Management with PowerShell

Security & AuditingDecember 23, 2024

Welcome to our PowerShell script section! In this post, we will share a script that simplifies the management of file and folder permissions. Properly managing permissions is crucial for maintaining security and ensuring that users have the appropriate access to the resources they need. Below is a sample PowerShell script that checks and updates permissions for a specified folder:

powershell
# Specify the folder path
$folderPath = "C:\Path\To\Your\Folder"
# Define the user and their access rights
$user = "DOMAIN\UserName"
$accessRights = "ReadAndExecute"
# Get the current ACL of the folder
$acl = Get-Acl -Path $folderPath
# Check if the user already has access
$existingRule = $acl.Access | Where-Object { $_.IdentityReference -eq $user }
if ($existingRule) {
    Write-Host "$user already has access with rights: $($existingRule.FileSystemRights)"
} else {
    # Create a new access rule
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user, $accessRights, "Allow")
    # Apply the new access rule
    $acl.AddAccessRule($accessRule)
    Set-Acl -Path $folderPath -AclObject $acl
    Write-Host "Permissions for $user have been added to $folderPath."
}

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.