active directory update Home folder path powershell
Get-ADUser -Filter * -SearchBase $targetou | Foreach-Object{ $sam = $_.SamAccountName Set-ADuser -Identity $_ -HomeDrive "H:" -HomeDirectory "\\server\share\$sam"
Here is what the above code is Doing:
1. It’s getting all the users in the target OU
2. It’s setting the home drive to H:
3. It’s setting the home directory to \\server\share\$sam
The problem is that the home directory is not being set to \\server\share\$sam. It’s being set to \\server\share\$_.SamAccountName. I’ve tried using $_.SamAccountName, $sam, and $($_.SamAccountName) but none of them work. I’ve also tried using the -replace operator to remove the $ from the variable but that doesn’t work either.
I’m sure this is a simple fix but I’m not sure what it is. Any help would be greatly appreciated.
Thanks!
A:
You need to use the -replace
operator to remove the $
from the variable.
$sam = $_.SamAccountName
Set-ADuser -Identity $_ -HomeDrive "H:" -HomeDirectory "\\server\share\$($sam.replace('$',''))"