MartinTheNob
MartinTheNob

Reputation: 73

Mapping Home folders using Powershell sets it in AD but does nothing else

I want my Active Directory users to have their own Home folder to store their files and I want to set this for each user account via PowerShell. I've tried several things but all it does is change the setting under "profile" in AD to the homefolder location, and this does not seem to show up for the users when they log in.

Some of the code i've tried:

    SET-ADUSER inggul –HomeDrive ‘P:’ –HomeDirectory ‘\\WIN-L372AGVF8AI\NewHome\$Inggul’

    $users = Get-ADUser -SearchBase "ou=sAdminOU,dc=kossi,dc=local" -Filter * -Properties Department
    foreach ($user in $users)
    {
    Set-ADUser $_.SamAccountName -HomeDrive "Z:" -HomeDirectory "\\WIN-L372AGVF8AI\NewHome\($_.SamAccountName)"
    }

Upvotes: 2

Views: 3139

Answers (2)

Mark Wragg
Mark Wragg

Reputation: 23355

Regarding your PowerShell, the first command looks like it should work to me. Your ForEach loop has an issue though, it's referencing $_ instead of $user. It should be:

foreach ($user in $users)
{
    Set-ADUser $user.SamAccountName -HomeDrive "Z:" -HomeDirectory "\\WIN-L372AGVF8AI\NewHome\$($user.SamAccountName)"
}

$_ is used to represent the current item in the pipeline within a ForEach-Object loop (or within other constructs that are handling input from the pipeline such as Where-Object for example). You are also missing a $ in front of ($user.SamAccountName). It needs to be $($user.SamAccountName), the $() part is the subexpression operator that ensures that theSamAccountNameproperty of$user` is correctly accessed before being converted to a string within the double quoted string.

Per the other answer, if the home directory still isn't being mapped on logon then you may also need to ensure that each user has share and NTFS permissions to their directory.

Upvotes: 2

henrycarteruk
henrycarteruk

Reputation: 13227

When you setup a Home Folder using the AD Console it also creates the users folder in the location specified (\\WIN-L372AGVF8AI\NewHome) and assigns the required permissions for the user to access the folder.

Using the PowerShell command does not create the folder or assign any permissions, it only updates the attributes in the users account.

You need to update your script to create the folder and set the permissions:

foreach ($user in $users)
{
$username = $user.SamAccountName
Set-ADUser $username -HomeDrive "Z:" -HomeDirectory "\\WIN-L372AGVF8AI\NewHome\$username"
New-Item -Path "\\WIN-L372AGVF8AI\NewHome\$username" -ItemType Directory
#Set appropriate ntfs permissions here
}

Upvotes: 1

Related Questions