Create Active Directory Users Home Folder and Assign Permissions with PowerShell

The following function is a work in progress, but I thought I would go ahead and share it.

This function requires a module named PowerShellAccessControl that was created by Rohn Edwards which is downloadable from the TechNet Script Repository. The version 3.0 beta revision of his module which is also downloadable on that same page is what was used to test the examples shown in this blog article.

#Requires -Version 3.0
#Requires -Modules ActiveDirectory, PowerShellAccessControl

function Create-MrUserFolder {

<#
.SYNOPSIS
Creates an Active Directory user's home folders and assigns modify permission
to the user and read permission to their manager.

.DESCRIPTION
Create-MrUserFolder is a function that is designed to create an Active Directory
user's home folder and assign full permissions to administrators and system,
modify to the user and read access to the user's manager.

.PARAMETER UserName
The Active Directory user account object for the user to create a folder for.

.PARAMETER Path
The root path location where to create the user folder.

.EXAMPLE
Get-ADUser -Identity MikeFRobbins -Properties Manager |
Create-MrUserFolder -Path 'S:\Users'

.INPUTS
Microsoft.ActiveDirectory.Management.ADAccount

.OUTPUTS
System.IO.DirectoryInfo

.NOTES
Author:  Mike F Robbins
Website: https://mikefrobbins.com
Twitter: @mikefrobbins
#>

[CmdletBinding()]
param (
[ValidateScript({Test-Path -Path $_ -PathType Container})]
[string]$Path,

[Parameter(Mandatory,
ValueFromPipeline)]
[Microsoft.ActiveDirectory.Management.ADAccount[]]$UserName
)

BEGIN {
$DefaultUsers = "$env:COMPUTERNAME\Administrators", 'System'
}

PROCESS {

foreach ($u in $UserName) {

$UserPath = Join-Path -Path $Path -ChildPath $u.SamAccountName

if (-not(Test-Path $UserPath -PathType Container)) {
New-Item -Path $UserPath -ItemType Directory
}
else {
Write-Warning -Message "'$UserPath' already exists."
}

$SecurityDescriptor = Get-SecurityDescriptor -Path $UserPath

foreach ($d in $DefaultUsers) {
$SecurityDescriptor | Add-AccessControlEntry -Principal $d -FolderRights FullControl -Apply -Force
}

$SecurityDescriptor | Add-AccessControlEntry -Principal $($u.UserPrincipalName) -FolderRights Modify -Apply -Force

if ($($u.Manager)) {
$SecurityDescriptor | Add-AccessControlEntry -Principal $((Get-ADUser -Identity $u.Manager).UserPrincipalName) -FolderRights ReadAndExecute -Apply -Force
}

Disable-AclInheritance -Path $UserPath -Force

}
}
}

The following example demonstrates creating home folders and assigning permissions to those folders for all of the users in the Northwind organizational unit in my test Active Directory environment:

Invoke-Command -ComputerName DC01 {
Get-ADUser -Filter * -SearchBase 'OU=Northwind Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' -Properties Manager |
Create-MrUserFolder -Path 'S:\Users'
}

homefolder1.jpg

Now to see if the permissions are correct, once again using a function from Rohn’s module:

Get-ChildItem -Path S:\Users | Get-AccessControlEntry | Format-Table -AutoSize

homefolder2.jpg

They look correct based on the previous information and verifying it against who each user’s manager is in Active Directory:

Get-ADUser -Filter * -SearchBase 'OU=Northwind Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' -Properties Manager, Title |
Select-Object -Property Name, Title, @{label='Manager';expression={$_.manager -replace '^CN=|\,.*$'}} |
Sort-Object -Property Manager, Name

homefolder3.jpg

Did you notice the regular expression that I used in the previous example to extract the managers name from the distinguished name that is returned by default?

The cool thing is you can actually create an Active Directory user account, create their home directory, and assign the proper permissions all in one command when the PassThru parameter of Get-ADUser is used to pass the user information to my function:

Invoke-Command -ComputerName DC01 {
New-ADUser -Name 'John Doe' -SamAccountName 'jdoe' -UserPrincipalName 'jdoe@mikefrobbins.com' -PassThru |
Create-MrUserFolder -Path 'S:\Users'
}

homefolder4.jpg

And to validate the permissions on the users folder were set correctly:

Get-AccessControlEntry S:\Users\jdoe

homefolder5.jpg

If you enjoyed this blog article and you live within driving distance of Baton Rouge Louisiana, you should consider attending SQL Saturday #324 on August 2nd. There’s an all day PowerShell track and I’ll be presenting two of the PowerShell sessions in that track. Rohn Edwards will also be presenting a session, his is on PowerShell and Access Control.

ยต