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:
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\testus er").
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!
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"
}
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\testus
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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\%userna
Here is the script I am using:
Open in new window