powershell script to create user folders and set permissions


  1. Posts : 2
    7
       #1

    powershell script to create user folders and set permissions


    Wondering if anyone in this forum can help me figure out what I am doing wrong with my script. I am grabbing a text file with a list of users in it. I want to then create folders for all those people and then set explicit permissions on those folders to only allow the users and admin access to it. So for the create user folder, I have...

    $Users = Get-Content "C:\Users.txt"
    ForEach ($user in $users)
    {
    $newPath = Join-Path \\mycomputer\Users$ -childpath $user
    New-Item $newPath -type directory
    }

    This works great as I now have 100 folders created. But how do I use something like....

    $acl = Get-Acl "\\mycomputer\users$"
    $permission = "mydomain\$users","FullControl","Allow"
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.SetAccessRule($accessRule)
    $acl | Set-Acl \\mycomputer\users$\$user

    When I try to loop these together it says the folder already exists or the setaccessrule identity references could not be translated.

    Anyone know how I loop it in with creating the folder? Or is there a way to just say...create folder based on this username, then take that username and make it the only admin on the folder along with the system admin??
      My Computer


  2. Posts : 2
    7
    Thread Starter
       #2

    answered my own problem...but maybe someone can help me out from here. This works for creating folders and setting permissions.

    $Users = Get-Content "C:\Users.txt"
    ForEach ($user in $users)
    {
    $newPath = Join-Path "\\myserver\Users$" -childpath $user
    New-Item $newPath -type directory

    $acl = Get-Acl $newpath
    $permission = "mydomain\$user","FullControl","Allow"
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.SetAccessRule($accessRule)
    $acl | Set-Acl $newpath
    }

    Anyone know how I would then look at an old server with user folders and do a stare and compare between the old and the new ones I just created? And once I find a folder that matches I want to grab all the contents in the old server and robocopy it over to the new server.
      My Computer


  3. Posts : 1
    België
       #3

    What does the users.txt-file looks like?
    I'm a powershell-newby ;-)

    Thanks
    Last edited by susxt; 09 Jul 2012 at 12:11.
      My Computer


  4. Posts : 1
    Windows 7 Home Premium x64
       #4

    "Anyone know how I would then look at an old server with user folders and do a stare and compare between the old and the new ones I just created?"

    Have you looked at Beyond Compare fromm Scooter Software. It was designed to do just what you are asking.

    Beyond Compare - The file compare, folder compare and remote directory compare utility from Scooter Software
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:54.
Find Us