SOLVED

Script to change permissions on multiple folders

Copper Contributor

I am involved in a project to migrate users H: drive data to OneDrive.  After we migrate their data to OneDrive, we want to change their H: drive folder and files to read-only.  I have found a cacls command to change the permissions (CACLS K:\foldername /T /E /C /P username:R) but I need to do this for specific user's H: drives.  o I need the script to read from a txt file to perform the cacls command on each of the folders in the txt file.  Is there a smart person out there that can help?

7 Replies
best response confirmed by craigbowens (Copper Contributor)
Solution

@craigbowens You could try something like using c:\temp\users.txt as input with a username per line. Not tested this, but I think it should work...

 

foreach ($user in get-content c:\temp\users.txt) {
cacls.exe "k:\$($user)" /T /E /C /P "$($user):R"
}

 

Did this work for you?

Yes!  Thank you very much!

Nice, please mark my answer as solution to mark it as solved

@Harm_Veenstra 

 

Hello - Thanks for that script as I was looking for the same. It worked. Can we have the same with icacls instead of cacls?

 

Can you also add a script that can give modify access only to one particular subfolder pls? subfolder name is the same for all users. 

I guess, but could you explain what the folder structure is? Perhaps using the NTFS module is easier? https://www.powershellgallery.com/packages/NTFSSecurity/4.2.4

@Harm_Veenstra 

 

Hi Thank You for your answer.

 

There will be one folder for each user (with their own names as folder name) under one main directory. They will have their own personal files in their folders. All such user folders will have their own content plus one folder with common name and this is created by IT for some application access.

 

I hope the below example explains it. 

 

ex:

p:\username1\operations

p:\username1\schedules

p:\username1\commonfoldername

 

p:\username2\AccountsPayable

p:\username2\AccountsReceivable

p:\username2\commonfoldername

 

I can try NTFS security but I am not a great scripter and I am going to take time to figure it out.

 

Immediate help from an expert will be great. So, please.

 

 

 

1 best response

Accepted Solutions
best response confirmed by craigbowens (Copper Contributor)
Solution

@craigbowens You could try something like using c:\temp\users.txt as input with a username per line. Not tested this, but I think it should work...

 

foreach ($user in get-content c:\temp\users.txt) {
cacls.exe "k:\$($user)" /T /E /C /P "$($user):R"
}

 

View solution in original post