Link to home
Start Free TrialLog in
Avatar of Leonardo Duarte
Leonardo Duarte

asked on

Script to disable inheritance of folders and subfolders and files

Hello friends, how are you?

I need a script that turns off the inheritance option for folders, subfolders, and files.

The structure of the folders is as follows:

Parent folder: FS
Child folders inside the FS folder:
COMERCIAL
ENGINEERING
SALES

I need the scripts to remove the inheritance folder from the FS folder in the COMMERCIAL, ENGINEERING, AND SALES folders and in the subfolders and files within them.

In other words the child folders can not inherit the parent's permission.

I need the script to do the disable inheritance action while retaining the permissions of the inherited FS folder.

Can anyone help?

User generated image
Avatar of NVIT
NVIT
Flag of United States of America image

>  I need the script to do the disable inheritance action while retaining the permissions of the inherited FS folder.
> I need the scripts to remove the inheritance folder from the FS folder in the COMMERCIAL, ENGINEERING, AND SALES folders and in the subfolders and files within them.

Do you need it for just COMMERCIAL, ENGINEERING, AND SALES folders? Are there adjacent folders next to those you want to avoid?

The following code disables inheritance for all folders under e:\fs, including their sub-folders.

1. Make a .bat file of the code.
2. Open a cmd window
3. Run the .bat file

IMPORTANT: It's best to test it on a test folder first, just to be sure. To do so, change the value to the right of Dir= to point to the test folder, e.g. Dir=e:\test

@echo off
REM Disable inheritance for all folders, including sub-folders

set Dir=e:\fs
for /r "%Dir%" %%a in (.) do (
  if /i "%%a" equ "%Dir%\." (
    echo skipping "%%a"
  ) else (
    icacls "%%a" /inheritance:d
  )
)

Open in new window

Avatar of Leonardo Duarte
Leonardo Duarte

ASKER

Hello NVIT,

I need the script to turn off inheritance of files as well, not just folders.

Can you adjust the script?
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial