We help IT Professionals succeed at work.

Script to disable inheritance of folders and subfolders and files

Leonardo Duarte
on
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?

File de sample
Comment
Watch Question

NVITEnd-user support
CERTIFIED EXPERT

Commented:
>  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

Author

Commented:
Hello NVIT,

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

Can you adjust the script?
End-user support
CERTIFIED EXPERT
Commented:
@echo off
REM Disable inheritance for all files and 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
    icacls "%%a\*" /inheritance:d
  )
)

Open in new window