Changing Folder Owner to Folder name from a CSV list of folders. RRS feed

  • Question

  • I have three questions on this script you provided. I need to be able to change the owner of a folder from an admins group back to the user which is the same name as the folder. I also need to pass down the permissions to all child objects. (The check box in the GUI). 

    First question. I also need to do this for specific folders so i cant run against the entire directory. And i'm having trouble getting the Folder names to pull from a CSV. Would be be able to provide me the syntax i would use via the Import-CSV command to do that with this code? Its just one colum named foldernames and all the folder name are in the first initial last name format. 

    $folders = Get-ChildItem -Path d:\home | Where-Object -FilterScript {
        $_.PSIsContainer -eq $true
    foreach ($folder in $folders) 
        $path = $folder.fullname
        $ACL = Get-Acl -Path $path
        $user = $folder.name
        icacls.exe $path /setowner $user

    2nd question. I noticed when i tried this script on one use. some of the files still had the domain security group assigned as owned and the user couldn't access them. There's a check box in the GUI to pass those ownership permissions to child objects as well. What would be the syntax for that? 

    3rd Question. I also need to pass the child object permissions down to make sure the new security group has access to all files and folders for recovery. I found an ICACLS command that works but i would need it to be repeatable. Perhaps adding it into this script? Something like this? 

    $folders = Get-ChildItem -Path d:\home | Where-Object -FilterScript { $_.PSIsContainer -eq $true } foreach ($folder in $folders) { $path = $folder.fullname $ACL = Get-Acl -Path $path $user = $folder.name icacls.exe $path /setowner $user
    icacls.exe $path /q /c /t /reset


    Thank you so much in advance for any assistance and guidance. 

    • Moved by Bill_Stewart Friday, March 15, 2019 8:11 PM This is not "train me in basics step-by-step in a forum" forum
    Sunday, February 17, 2019 2:32 PM

All replies

  • You say "we provided".  We do not provide scripts.  This is a script you copied from a post. YOU need to provide some explanation of what you are doing and you need to provide a script that you have written or are writing.

    What is it you want to do and post you script.

    Also the code posted is really many lines of unnecessary code.  The following is the correct code:

    Get-ChildItem -Path d:\home -Directory |
            icacls $path /setowner $_.Name
            icacls $_.Name /q /c /t /reset   

    Also the command being used is a system utility.  Posting in the Windows General forum would be the correct place to ask questions about system utilities.

     We cannot design a solution for you.


    • Edited by jrv Sunday, February 17, 2019 7:10 PM
    Sunday, February 17, 2019 7:09 PM