Skip to content

Instantly share code, notes, and snippets.

@timsonner
Last active January 29, 2025 07:30
Show Gist options
  • Select an option

  • Save timsonner/f34c1868021f925a9f47059ec718e16e to your computer and use it in GitHub Desktop.

Select an option

Save timsonner/f34c1868021f925a9f47059ec718e16e to your computer and use it in GitHub Desktop.
Modifying file access controls using PowerShell instead of ''takeown' and 'icacls'

Modify file Access Controls with PowerShell

# Take ownership of the file
$filePath = (Read-Host "Filepath")
$acl = Get-Acl -Path $filePath
$acl.SetOwner([System.Security.Principal.NTAccount]$(Read-Host "Username"))
Set-Acl -Path $filePath -AclObject $acl
Get-Acl -Path $filePath
# Set Access Controls of the file
$filePath = (Read-Host "Filepath")
$acl = Get-Acl -Path $filePath
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($(Read-Host "Username"), "FullControl", "Allow")
$acl.SetAccessRule($accessRule)
Set-Acl -Path $filePath -AclObject $acl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment