Set Owner recursively

I have been reading the Scripting Guy's post on setting file ownership for a specific user.

His code is:

$objUser = New-Object System.Security.Principal.NTAccount("fabrikam", "kenmyer")
$objFile = Get-Acl C:\Scripts\Test.txt
Set-Acl -aclobject $objFile -path C:\Scripts\Test.txt

Open in new window

This works great for a specific folder. I am trying to get it to work recursively so that all subfolders and files have the same specified owner.

How can this be done?
LVL 22
Joseph MoodyBlogger and wearer of all hats.Asked:
Who is Participating?
Rajitha ChimmaniConnect With a Mentor Commented:
Try using the code given in link below

$acct1 = New-Object System.Security.Principal.NTAccount("fabrikam", "kenmyer")
$profilefolder = Get-Item MyFolder
$acl1 = $profilefolder.GetAccessControl()
dir -r c:\Users\goyuix\temp | set-acl -aclobject $acl1


Alternate option given in link below
Joseph MoodyBlogger and wearer of all hats.Author Commented:
You are awesome!! Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.