Changing file permissions using PowerShell
Use the following script for changing file permissions using PowerShell. I use this script when command line takeown and icacls fail. Obviously, run from an elevated PowerShell prompt or by typing 'powershell' from elevated cmd.
The Script
Change $folder to the base directory you want for changing permissions.
Please note, you can use network UNC paths for this or a simple C:.
$folder = "\homeserver\users"
$users = get-childitem $folder
Foreach ($user in $users) {
$acl = Get-Acl $user.FullName
$acl.SetOwner([System.Security.Principal.NTAccount]"$user")
set-acl $user.FullName $acl -Verbose
$subFolders = Get-ChildItem $user.FullName -Directory -Recurse
Foreach ($subFolder in $subFolders) {
$acl = Get-Acl $subFolder.FullName
$acl.SetOwner([System.Security.Principal.NTAccount]"$user")
set-acl $subFolder.FullName $acl -Verbose
}
$subFiles = Get-ChildItem $user.FullName -File -Recurse
Foreach ($subFile in $subFiles) {
$acl = Get-Acl $subFile.FullName
$acl.SetOwner([System.Security.Principal.NTAccount]"$user")
set-acl $subFile.FullName $acl -Verbose
}
}
I've never had takeown fail on local disks, but I have found that I needed to resort to using a PowerShell script when doing this through a network location. It's a very powerful script, however always be careful when running this. Be sure to always test this in a subdirectory first. This can be catastrophic for a company with millions of files and folder to reset the permission on, therefore be very careful!
Leave any Questions and Comments below and I will get back to you. I regularly publish on YouTube, Steemit, and christitus.com so if you’d like to see more videos please like, subscribe, and follow me.
Posted from my blog: https://www.christitus.com/2018/06/14/changing-file-permissions/
Leave Changing file permissions using PowerShell to:
Read more #technology posts
Best Posts From Chris Titus
We have not curated any of christitus's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Chris Titus
- Resize LVM Partition | Fedora 29 | Step by Step Guide
- Grant Full Access to all Public Folders for a user in Exchange
- Force active directory sync to Office 365 with Azure ADSync
- Create Swap file on Cloud VM (AWS, GCloud, or Azure)
- Cloud migration hosting in Google, Amazon, or Microsoft
- Launching a startup program to run as administrator
- Troubleshooting Nagios 4 Core Installation
- Changing file permissions using PowerShell
- Commiting Changes to Docker images and containers
- No System Reserved Partition, Boot device not found, corrupt BCD / MBR