Forum Discussion
Powershell script to enable inheritance for the folders created in NTFS share
- Sep 30, 2020
You can use this example
$x=Get-ItemProperty -Path C:\MyFolder\123\456 Write-Host "The Parent is " -ForegroundColor Green -NoNewline $x.Parent.Name
if the answer fulfill your needs, please don't forget to click on Best Response and give a like 🙂
Thanks
Thank you farismalaeb .
The script worked after changing the path from '\\Windows-2S9QPLV\OIMShare1 to C:\OIMShare1.
Quick question :
Is there a way to identify the parent folder if we provide the child items in the script.
Actually, we will provide the child items ('C:\OIMShare1\abc1\') in the script and it has identify the parent folder ('C:\OIMShare1\') and the child items need to inherit all the permissions for the parent.
Thanks in advance
You can use this example
$x=Get-ItemProperty -Path C:\MyFolder\123\456
Write-Host "The Parent is " -ForegroundColor Green -NoNewline
$x.Parent.Name
if the answer fulfill your needs, please don't forget to click on Best Response and give a like 🙂
Thanks
- suren424Oct 11, 2020Copper Contributor
Thank youfarismalaeb for all the help and guidance.
- farismalaebOct 06, 2020Iron Contributor
Everything seems fine, and I always recommend trying things before applying it in production, what might seems to be fine might not be OK with you, so its always better to try 🙂
and yes you can get the current permission by running the following
Get-Acl -Path C:\AttendanceReport\ | select path,access -ExpandProperty Access
there is a more complicated way, which will provide a cleaner output, but you just need this for reference, so enjoy you day and the script
-----------------------------------
If everything is fine, please select one of the response and click on Best Response 🙂
Thanks
- suren424Oct 01, 2020Copper Contributor
Thank you farismalaeb.
The script worked. I was able to get the parent for the child items and set the permissions.
-- Does the below script looks good. As the permissions section is in a loop, just wanted to make sure, i am not redoing the same thing again and again.
-- Is there a way to print the permissions before and after enabling the inheritance to know what permissions have changed ?
$the_shares = ('C:\OIMShare\abc\', 'C:\OIMShare\abc1\','C:\OIMShare1\def\','C:\OIMShare2\ghi\') $allFolders=Get-ItemProperty -Path $the_shares Write-Host "The Parent is " -ForegroundColor Green -NoNewline $allFolders.Parent.Name foreach ($Folder in $allFolders){ $Permission=get-acl -Path $Folder.FullName $Permission.SetAccessRuleProtection($False,$true) Set-Acl -Path $Folder.FullName -AclObject $Permission }
Thank you very much for all you guidance
- suren424Oct 01, 2020Copper Contributor
Thank youfarismalaeb
It worked. Can i print what permissions is this script setting and what were there on the child folder before ?
And does this below script looks fine or am i missing anything?
I am trying to get the parent folder for the child item and inherit the permissions. Just wanted to make sure, i am not redoing the same thing as it is in the for loop.
Script :
$the_shares = ('C:\OIMShare\abc\', 'C:\OIMShare\abc1\','C:\OIMShare1\def\','C:\OIMShare2\ghi\')
$allFolders=Get-ItemProperty -Path $the_shares
Write-Host "The Parent is " -ForegroundColor Green -NoNewline
$allFolders.Parent.Name
foreach ($Folder in $allFolders){
$Permission=get-acl -Path $Folder.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $Folder.FullName -AclObject $Permission
}Thanks in advance.