Setting AD Home Directory for user objects by Powershell script doesn't create folder?


We recently purchased a NAS unit and got it configured and online. There are a couple of network share folders set up, one of which is called "Employees" intended to be the location for each desk user's personal home folder.  

However, I would like to avoid having to drill into the properties of each user object to set their home directory each time we set up a new user.
I searched and came up with the following PowerShell script to filter AD Users by desired department (the ones who actually sit at a desk and log in with their AD username) and then set their home directory like we want:

get-aduser -filter {department -like '999*'} | Foreach-Object{
$sam = $_.SamAccountName
Set-ADuser -Identity $_ -homedrive "x:" -homedirectory "\\server\Employees\$sam"

Open in new window

This script works perfect fine, and resolves the $sam to the correct folder name. The only issue I am seeing is that setting the path this way does not create the folder if it is not already present!

If I use the AD Users & Computers tool, and set the path via the properties, the nonexistent folder is created immediately when I click OK or Apply.

If I set the path via the script, this does not happen.  I can go into the Properties in Users and Computers and verify it filled out the Connect To field on the Profile tab, but the folder is not created, even after clicking OK or Apply.

This is the case no matter what I set the path to, even if I explicitly name it in the PS script (-homedirectory "\\server\Employees\testuser").

Any advice? Is there an easier way to deploy the kind of automatic configuration we desire?

Alternatively, if there is a line of script that I can insert in the Foreach-Object to create this folder "if not exist" I would settle for that.

Thanks for the help!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BMFCAuthor Commented:
I was able to customize the script further to include the command to create a folder of the account name.

This is an acceptable solution, but I will have to run this every time I add a new user.

If anyone else can come up with a way to automatically create a folder on the network at \\server\employees\%username% and fill this path in the Home Directory for each user AS THEY ARE CREATED this would be the ideal solution.

Here is the script I am using:

get-aduser -filter {department -like '999*'} | foreach-object{
$sam = $_.SamAccountName
$destDir = "\\server\Employees\$sam"
If (!(Test-Path $destDir)) {
new-item -path $destDir -ItemType Directory
Set-ADuser -Identity $_ -homedrive "x:" -homedirectory "\\server\employees\$sam"

Open in new window

Michael PfisterCommented:
There is no automatic way to create the home Folder.
Your PowerShell script is a good solution but it should also set the correct permissions to the user home folder.

new-item -path $destDir -ItemType Directory
Add-NTFSAccess -Path $destDir  -Account $sam -AccessRights Modify



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.