PowerShell creates folders and grants Owner permissions to specific users
Some time ago, I experienced a file server migration, in which there were many users' redirected folders, such as desktops, documents, pictures, etc.
Because of the large number, the process of repeated creation and authorization of each folder is very troublesome, so a script is written.
First of all, fill in the user name ~ ~ in a document, that is, the account name of the user for whom we want to create a folder
# Import account information
$UserName = Get-Content D:\ UserName.txt
# traversing account information
Foreach ($User in $UserName)
{
# create a folder locally that is the same as the user name
New-Item-Path "E:\ Userhome` $"-ItemType Directory-Name $User
# define folder permissions
$Ar = New-Object System.Security.AccessControl.FileSystemAcce***ule ("Contoso\ $($User)", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
# modify folder permissions
$Acl = Get-Acl-Path "E:\ Userhome` $\ $($User)"
$Acl.SetAcce***ule ($Ar)
Set-Acl-Path "E:\ Userhome` $\ $($User)"-AclObject $Acl
}
Done, now you have a lot of folders with the same account name and only the user has full control.