Link to home
Start Free TrialLog in
Avatar of BMFC
BMFC

asked on

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

Hi,

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!
Avatar of BMFC
BMFC

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial