SOLVED

Powershell script to enable inheritance for the folders created in NTFS share

%3CLINGO-SUB%20id%3D%22lingo-sub-1706352%22%20slang%3D%22en-US%22%3EPowershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1706352%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20have%20a%20NTFS%20Share%20folder%20wherein%20we%20are%20creating%20all%20the%20users'%20homeDirectories%20(homefolder)%20within%20the%20enterprise%20using%20Oracle%20identity%20management%20tool.%20Homefolder%20creation%20is%20working%20good.%20But%2C%20we%20are%20having%20issues%20with%20the%20permissions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E--%20It%20doesn't%20inherit%20permissions%20from%20the%20parent%20folder.%3C%2FP%3E%3CP%3E--%20sometimes%20the%20owner%20also%20doesn't%20get%20the%20permissions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20for%20us%20to%20check%20the%20current%20permissions%20and%20enable%20inheritance%20for%20the%20folders%20created%20in%20the%20share%20and%20all%20the%20child%20items%20within.%20We%20have%20a%20lot%20of%20homefolders%20and%20not%20able%20to%20figureout%20how%20to%20do%20it%20via%20Powershell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20input%20will%20be%20greatly%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1706352%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eenable%20inheritance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1709671%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1709671%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20the%20quick%20response%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20was%20doing%20some%20research%20and%20network%20share%20folder%20we%20need%20to%20have%20'NTFSSecurity%20'%20powershell%20module.%20Do%20i%20have%20to%20use%20cmdlets%20from%20'NTFSSecurity'%20module%20or%20the%20one%20you%20shared%20works%20fine%20too.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20used%20this%20enable%20inheritance%20for%20'abc'%20folder%2C%20but%20it's%20not%20enabling%20inheritance%20for%20the%20child%20items%20in%20the%20'abc'%20holder.%20The%20same%20goes%20with%20child%20items%20in%20the%20parent%20folder%20'C%3A%5CShare'.%20How%20to%20enable%20inheritance%20for%20child%20items%20too.%20I%20want%20to%20do%20this%20for%20all%20the%20folders%20that%20gets%20created%20newly%20on%20a%20daily%20basis.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EGet-Item%20C%3A%5CShare%5Cabc%5C%20%7C%20Enable-NTFSAccessInheritance%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1710066%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1710066%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F806703%22%20target%3D%22_blank%22%3E%40suren424%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegardless%2C%3C%2FP%3E%3CP%3EThe%20Modules%20are%20make%20to%20ease%20and%20make%20common%20tasks%20easier%20and%20possible.%3C%2FP%3E%3CP%3EHave%20you%20tried%20the%20code%20I%20wrote%2C%3C%2FP%3E%3CP%3EGive%20it%20a%20try%20on%20a%20test%20folder%20and%20see%20the%20result%2C%20I%20test%20it%20from%20my%20side%20and%20its%20working%20fine%2C%20this%20is%20a%20native%20code%2C%20so%20no%20need%20for%20any%20module%20or%20anything%2C%20just%20copy%20and%20paste%20and%20thats%20it.%3C%2FP%3E%3CP%3EReplace%20the%20Value%20or%20the%20%24Path%20with%20any%20folder%20and%20give%20it%20a%20try.%20if%20it%20work%20(and%20should)%20you%20can%20add%20it%20with%20a%20foreach%20loop.%20Just%20try%20the%20code%20on%20a%20test%20folder%20and%20post%20the%20result%20back%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1711410%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1711410%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%26nbsp%3B%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20i%20tried%20the%20below%20script%20%2C%20it%20didn't%20set%20enable%20inheritance%20for%20the%20child%20items%20in%20'OIMShare'%20folder.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24path%3D'%5C%5CWINDOWS-2S9Q%5COIMShare%5C'%3CBR%20%2F%3E%24Permission%3Dget-acl%20-Path%20%24path%3CBR%20%2F%3E%24Permission.SetAccessRuleProtection(%24False%2C%24true)%3CBR%20%2F%3ESet-Acl%20-Path%20%24path%20-AclObject%20%24Permission%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%2C%20it%20worked%20when%20i%20ran%20this%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24path%3D'%5C%5CWINDOWS-2S9QPLV%5COIMShare%5Cabc%5C'%3CBR%20%2F%3E%24Permission%3Dget-acl%20-Path%20%24path%3CBR%20%2F%3E%24Permission.SetAccessRuleProtection(%24False%2C%24true)%3CBR%20%2F%3ESet-Acl%20-Path%20%24path%20-AclObject%20%24Permission%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20have%20lot%20parent%20folders%20like%20'OIMShare'%20(OIMShare1%2C%20OIMShare2%2C....)%20which%20many%20child%20items%20in%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1714584%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1714584%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYour%20script%20worked.%20I%20tried%20improvising%20your%20script%20to%20include%20multiple%20shares%2C%20but%20it's%20throwing%20below%20error(attached%20screeshot).%26nbsp%3B%20Am%20i%20missing%20anything%20in%20the%20script%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22suren424_0-1601064182231.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F222173i0FC152BF167269BD%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22suren424_0-1601064182231.png%22%20alt%3D%22suren424_0-1601064182231.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EScript%20%3A%3C%2FP%3E%3CP%3E%24the_shares%20%3D%20('%5C%5CWINDOWS-2S9QPLV%5COIMShare%5C'%2C%20'%5C%5CWINDOWS-2S9QPLV%5COIMShare1%5C')%3CBR%20%2F%3E%24allFolders%3DGet-ChildItem%20-Path%20%24the_shares%20-Directory%20-Recurse%3CBR%20%2F%3Eforeach%20(%24Folder%20in%20%24allFolders)%7B%3CBR%20%2F%3E%24Permission%3Dget-acl%20-Path%20%24Folder.FullName%3CBR%20%2F%3E%24Permission.SetAccessRuleProtection(%24False%2C%24true)%3CBR%20%2F%3ESet-Acl%20-Path%20%24Folder.FullName%20-AclObject%20%24Permission%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1731511%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1731511%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%26nbsp%3B.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20worked%20after%20changing%20the%20path%20from%20'%3CSPAN%3E%5C%5CWindows-2S9QPLV%5COIMShare1%20to%20C%3A%5COIMShare1.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EQuick%20question%20%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EIs%20there%20a%20way%20to%20identify%20the%20parent%20folder%20if%20we%20provide%20the%20child%20items%20in%20the%20script.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EActually%2C%20we%20will%20provide%20the%20child%20items%20('C%3A%5COIMShare1%5Cabc1%5C')%20in%20the%20script%20and%20it%20has%20identify%20the%20parent%20folder%20('C%3A%5COIMShare1%5C')%20and%20the%20child%20items%20need%20to%20inherit%20all%20the%20permissions%20for%20the%20parent.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks%20in%20advance%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1731567%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1731567%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F806703%22%20target%3D%22_blank%22%3E%40suren424%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20use%20this%20example%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24x%3DGet-ItemProperty%20-Path%20C%3A%5CMyFolder%5C123%5C456%0AWrite-Host%20%22The%20Parent%20is%20%22%20-ForegroundColor%20Green%20-NoNewline%0A%24x.Parent.Name%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Eif%20the%20answer%20fulfill%20your%20needs%2C%20please%20don't%20forget%20to%20click%20on%20Best%20Response%20and%20give%20a%20like%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1732877%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1732877%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20worked.%20Can%20i%20print%20what%20permissions%20is%20this%20script%20setting%20and%20what%20were%20there%20on%20the%20child%20folder%20before%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20does%20this%20below%20script%20looks%20fine%20or%20am%20i%20missing%20anything%3F%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20get%20the%20parent%20folder%20for%20the%20child%20item%20and%20inherit%20the%20permissions.%20Just%20wanted%20to%20make%20sure%2C%20i%20am%20not%20redoing%20the%20same%20thing%20as%20it%20is%20in%20the%20for%20loop.%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CU%3EScript%20%3A%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24the_shares%20%3D%20('C%3A%5COIMShare%5Cabc%5C'%2C%20'C%3A%5COIMShare%5Cabc1%5C'%2C'C%3A%5COIMShare1%5Cdef%5C'%2C'C%3A%5COIMShare2%5Cghi%5C')%3CBR%20%2F%3E%24allFolders%3DGet-ItemProperty%20-Path%20%24the_shares%3CBR%20%2F%3EWrite-Host%20%22The%20Parent%20is%20%22%20-ForegroundColor%20Green%20-NoNewline%3CBR%20%2F%3E%24allFolders.Parent.Name%3CBR%20%2F%3Eforeach%20(%24Folder%20in%20%24allFolders)%7B%3CBR%20%2F%3E%24Permission%3Dget-acl%20-Path%20%24Folder.FullName%3CBR%20%2F%3E%24Permission.SetAccessRuleProtection(%24False%2C%24true)%3CBR%20%2F%3ESet-Acl%20-Path%20%24Folder.FullName%20-AclObject%20%24Permission%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1733027%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1733027%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20worked.%20I%20was%20able%20to%20get%20the%20parent%20for%20the%20child%20items%20and%20set%20the%20permissions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E--%20Does%20the%20below%20script%20looks%20good.%20As%20the%20permissions%20section%20is%20in%20a%20loop%2C%20just%20wanted%20to%20make%20sure%2C%20i%20am%20not%20redoing%20the%20same%20thing%20again%20and%20again.%3C%2FP%3E%3CP%3E--%20Is%20there%20a%20way%20to%20print%20the%20permissions%20before%20and%20after%20enabling%20the%20inheritance%20to%20know%20what%20permissions%20have%20changed%20%3F%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%20%24the_shares%20%3D%20('C%3A%5COIMShare%5Cabc%5C'%2C%20'C%3A%5COIMShare%5Cabc1%5C'%2C'C%3A%5COIMShare1%5Cdef%5C'%2C'C%3A%5COIMShare2%5Cghi%5C')%0A%24allFolders%3DGet-ItemProperty%20-Path%20%24the_shares%0AWrite-Host%20%22The%20Parent%20is%20%22%20-ForegroundColor%20Green%20-NoNewline%0A%24allFolders.Parent.Name%0Aforeach%20(%24Folder%20in%20%24allFolders)%7B%0A%24Permission%3Dget-acl%20-Path%20%24Folder.FullName%0A%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0ASet-Acl%20-Path%20%24Folder.FullName%20-AclObject%20%24Permission%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20very%20much%20for%20all%20you%20guidance%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi,

 

We have a NTFS Share folder wherein we are creating all the users' homeDirectories (homefolder) within the enterprise using Oracle identity management tool. Homefolder creation is working good. But, we are having issues with the permissions.

 

-- It doesn't inherit permissions from the parent folder.

-- sometimes the owner also doesn't get the permissions.

 

Is there a way for us to check the current permissions and enable inheritance for the folders created in the share and all the child items within. We have a lot of homefolders and not able to figureout how to do it via Powershell.

 

Any input will be greatly appreciated.

 

Thanks in advance.

 

 

 

13 Replies

@suren424 

Try this

 

$path='\\SharePath\Whatever'
$Permission=get-acl -Path $path
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $path -AclObject $Permission
#https://docs.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.objectsecurity.setaccessru...

 

This will enable inheritance on a folder

Thanks for the quick response @farismalaeb

 

I was doing some research and network share folder we need to have 'NTFSSecurity ' powershell module. Do i have to use cmdlets from 'NTFSSecurity' module or the one you shared works fine too.

 

I used this enable inheritance for 'abc' folder, but it's not enabling inheritance for the child items in the 'abc' holder. The same goes with child items in the parent folder 'C:\Share'. How to enable inheritance for child items too. I want to do this for all the folders that gets created newly on a daily basis.

 

Thanks in advance.

 

Get-Item C:\Share\abc\ | Enable-NTFSAccessInheritance

@suren424 

Regardless,

The Modules are make to ease and make common tasks easier and possible.

Have you tried the code I wrote,

Give it a try on a test folder and see the result, I test it from my side and its working fine, this is a native code, so no need for any module or anything, just copy and paste and thats it.

Replace the Value or the $Path with any folder and give it a try. if it work (and should) you can add it with a foreach loop. Just try the code on a test folder and post the result back

 

 

Thank you  @farismalaeb

 

When i tried the below script , it didn't set enable inheritance for the child items in 'OIMShare' folder.

 

$path='\\WINDOWS-2S9Q\OIMShare\'
$Permission=get-acl -Path $path
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $path -AclObject $Permission

 

 

But, it worked when i ran this script.

 

$path='\\WINDOWS-2S9QPLV\OIMShare\abc\'
$Permission=get-acl -Path $path
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $path -AclObject $Permission

 

We have lot parent folders like 'OIMShare' (OIMShare1, OIMShare2,....) which many child items in it.

 

Thanks in advance.

@suren424 

 

Yes sure it wont work in all folder as its not part of a loop

This is the full code

$allFolders=Get-ChildItem -Path C:\MySource -Directory -Recurse
foreach ($Folder in $allFolders){
$Permission=get-acl -Path $Folder.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $Folder.FullName -AclObject $Permission
}

You only need to modify the path of the root and the script will do the same

Thank you@farismalaeb

 

Your script worked. I tried improvising your script to include multiple shares, but it's throwing below error(attached screeshot).  Am i missing anything in the script ?

 

suren424_0-1601064182231.png

 

Script :

$the_shares = ('\\WINDOWS-2S9QPLV\OIMShare\', '\\WINDOWS-2S9QPLV\OIMShare1\')
$allFolders=Get-ChildItem -Path $the_shares -Directory -Recurse
foreach ($Folder in $allFolders){
$Permission=get-acl -Path $Folder.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $Folder.FullName -AclObject $Permission
}

 

 

@suren424 

The error is stating that the path not found.

Make sure that you have the access to that directory and/or its really exist \\Windows-2S9QPLV\OIMShare1

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

best response confirmed by suren424 (Occasional Contributor)
Solution

@suren424 

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 

 

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.

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

@suren424 

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

 

Thank you@farismalaeb for all the help and guidance.