4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

Changing file permissions using PowerShell

https://www.christitus.com/wp-content/uploads/2018/06/changing-file-permissions.png

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-childitemusers = get-childitemfolder Foreach (userinuser inusers) { acl=Get-Aclacl = Get-Acluser.FullName acl.SetOwner([System.Security.Principal.NTAccount]"acl.SetOwner([System.Security.Principal.NTAccount]"user") set-acl user.FullNameuser.FullNameacl -Verbose subFolders=Get-ChildItemsubFolders = Get-ChildItemuser.FullName -Directory -Recurse Foreach (subFolderinsubFolder insubFolders) { acl=Get-Aclacl = Get-AclsubFolder.FullName acl.SetOwner([System.Security.Principal.NTAccount]"acl.SetOwner([System.Security.Principal.NTAccount]"user") set-acl subFolder.FullNamesubFolder.FullNameacl -Verbose } subFiles=Get-ChildItemsubFiles = Get-ChildItemuser.FullName -File -Recurse Foreach (subFileinsubFile insubFiles) { acl=Get-Aclacl = Get-AclsubFile.FullName acl.SetOwner([System.Security.Principal.NTAccount]"acl.SetOwner([System.Security.Principal.NTAccount]"user") set-acl subFile.FullNamesubFile.FullNameacl -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/

3 upvotes $0.00

Replies (1)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: