Ïn our current AD setup, we have under AD user Home Folder, setup a redirection folder and mapped it H:
We made this because of backup and roaming profiles etc.
Now i need to change this setup to local folder - empty datafield, because we are moving to OneDrive!.
I am looking for a powershell script, which can bulk change the mapped drive to empty local folder.
The list of usernames is about 2100, and i dont want to edit this manually.
#define where the users are located in the AD
$OUsearchbase = 'OU=Users,DC=company,DC=nl', 'OU=Users2,DC=company,DC=nl', 'OU=Users3,DC=company,DC=nl'
$users = Get-ADUser -Filter * -SearchBase ($OUsearchbase)
foreach ($user in $users)
{
Set-ADUser -Identity $user -HomeDirectory ''
}
$inWrongOu = @()
foreach($user in $users)
{
$an = $user.initial
try {
# Get USER from AD
$usrinfo = get-aduser $an -Properties *
$loc = $usrinfo.l
$usrHomedir = $usrinfo.HomeDirectory
Add-Content -Path $logfile “User: $an; Homedirectory: $usrHomedir”
$usrou = $usrinfo.distinguishedname
$ou = $usrou.substring($usrou.length - 47)
#write-host $ou -ForegroundColor Cyan
# Test if user is in OU=EGVer2
# Just for info - Homedirectory will be remove anyway
if($ou -ne $correctOU)
{
Write-Host $an "is not in the correct OU -" $usrou -ForegroundColor Yellow
Add-Content -Path $logfile "User: $an is NOT in the correct OU!"
add-content -path $oufile "$an;$usrou"
$inWrongOu += $an
}
else
{
Write-host "$an is in correct OU - $ou" -ForegroundColor Green
# Remove HomeDrive and HomeDirectory from User
}
}
Catch {
write-host $Error[0].Exception.Message -ForegroundColor Red
}
When you are removing values in AD you should use the -Clear switch to set that value to NULL. Set-ADUser | Microsoft Learn
Clear
Specifies an array of object properties that will be cleared in the directory. Use this parameter to clear one or more values of a property that cannot be modified using a cmdlet parameter. To modify an object property, you must use the LDAP display name. You can modify more than one property by specifying a comma-separated list. The format for this parameter is
-Clear Attribute1LDAPDisplayName, Attribute2LDAPDisplayName
For example, if you want to clear the value for the Phone-Office-Other attribute (LDAP display name ‘otherTelephone’) set the Clear parameter as follows.
Are there any users in the directory that you do NOT want to set? You can simply test one user to validate it’s doing what you expect, preferably a non-Prod user:
No, all users in the OU i need to change settings.
So the command “Set-ADUser -Clear homeDirectory” sets the home folder to “local path - No data in the field”
I have tried to understand if your scripts does what i need them to do. Im sorry but im not that skilled in powershell.
I have made a mockup in a jpg file, to illustrate what i need the script to do.