Welcome › Forums › General PowerShell Q&A › change permissions to folder root and child files and folders
- This topic has 6 replies, 3 voices, and was last updated 3 months ago by
Participant.
-
AuthorPosts
-
-
September 7, 2020 at 5:30 am #254948
Hello.
Thanks in advance for assistance with this.
With the below script I am trying to ammend folder permissions for folders, child folders and files from within that folder also. I can only seem to ge 5 child folders depp though. I would like to recurivley run this to ensure all child folder permissions are ammended.
Code:
PowerShell12345678910111213141516$folders = "\\server\home\test"$serviceAccount = "isad\svc_test"$childfolders = Get-ChildItem -Path $folders -Force$grandchildfolders = Get-ChildItem -Path $childfolders -Force$greatgrandchildfolders = Get-ChildItem -Path $grandchildfolders -Force$2greatgrandchildfolders = Get-ChildItem -Path $greatgrandchildfolders -ForceAdd-NTFSAccess -Account $serviceAccount -path $folders -AccessRights FullControl -AppliesTo ThisFolderSubfoldersAndFiles$childfolders | Add-NTFSAccess -Account $serviceAccount -AccessRights FullControl -AppliesTo ThisFolderSubfoldersAndFiles$grandchildfolders | Add-NTFSAccess -Account $serviceAccount -AccessRights FullControl -AppliesTo ThisFolderSubfoldersAndFiles$greatgrandchildfolders | Add-NTFSAccess -Account $serviceAccount -AccessRights FullControl -AppliesTo ThisFolderSubfoldersAndFiles$2greatgrandchildfolders | Add-NTFSAccess -Account $serviceAccount -AccessRights FullControl -AppliesTo ThisFolderSubfoldersAndFiles-
This topic was modified 4 months, 1 week ago by
jimleslie7778.
-
This topic was modified 4 months, 1 week ago by
kvprasoon.
-
This topic was modified 4 months, 1 week ago by
-
September 7, 2020 at 3:27 pm #255086
What you would probably need is to use -Recurse switch parameter of Get-ChildItem cmdlet then use ForEach-Object cmdlet to iterate through each folder item coming through.
If you have not used these options, I suggest you to read documentation using Get-Help ForEach-Object -Full cmdlet. -
October 11, 2020 at 10:27 am #262463
thanks for the help, i have added the -recurse itch but this still seems to fail for child folders, whilst sucessfully ammending the first folder and files within there, any ideas?
-
October 12, 2020 at 1:04 am #262586
I find the -Recurse parameter of Get-ChildItem to be a little slow. I also like recursive functions. This function will go through and set each folder and file recursively. You simply pass it the root folder to start with and the account. I’ve also prepared it to take AccessRights and AppliesTo as a parameter with the default being what you were trying to achieve. If you want to see the items as they are being set then add -Verbose. I turned verbose off on the NTFSAccess command as it was very, very noisy.
PowerShell123456789101112131415161718192021222324252627282930313233343536Function Set-RecursiveACL {[cmdletbinding()]Param([Parameter(Position=0,ValueFromPipeline,Mandatory)]$Folder,[Parameter(Position=1,Mandatory)][string]$Account,[Parameter(Position=2)][string[]]$AccessRights = 'FullControl',[Parameter(Position=3)][string]$AppliesTo = 'ThisFolderSubfoldersAndFiles')process{if($Folder -is [string]){$Folder = Get-Item $Folder}Write-Verbose "Granting $($AccessRights) rights on $($Folder.FullName) to $($Account)"$Folder | Add-NTFSAccess -Account $Account -AccessRights $AccessRights -AppliesTo $AppliesTo -AccessType Allow -Verbose:$falseGet-ChildItem -Path $Folder.FullName -File -Force | ForEach-Object {Write-Verbose "Granting $($AccessRights) rights on $($_.FullName) to $($Account)"$_ | Add-NTFSAccess -Account $Account -AccessRights $AccessRights -AppliesTo $AppliesTo -AccessType Allow -Verbose:$false}Get-ChildItem -Path $Folder.FullName -Directory -Force | Set-RecursiveACL -Account $Account}}$rootfolder = "\\server\home\test"$serviceAccount = "isad\svc_test"$rootfolder | Set-RecursiveACL -Account $serviceAccount -Verbose -
October 12, 2020 at 10:35 am #262691
would this have the ability to apply those permissions without modifying the folder owners? as these are homefolders each folder owner needs to be the individual users
-
October 12, 2020 at 12:56 pm #262718
It’s your command… you tell me. I only used the command you were already using. Plus, you could simply test it and see..
-
October 14, 2020 at 4:30 am #263214
Fair point, I test and no it doesnt change the folder owners, massive bonus there!!
one further question, is there a way of pulling several (possibly hundreds) of folders from a csv or text file then applying this to them?
-
-
AuthorPosts
- The topic ‘change permissions to folder root and child files and folders’ is closed to new replies.