Author Posts

March 13, 2017 at 12:54 pm

Hi,

Ï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. :/

Can anyone help me setup a script like this? 🙂

Best regards

March 13, 2017 at 1:09 pm

Hi Brian,

Is there something in common for the users so you can target the users? All in the same OU for example?

Albert

March 13, 2017 at 1:19 pm

Brian,

If so you can use try script

#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 ''
}

I have not tested it but it should work.

March 13, 2017 at 1:26 pm

Hi,

I have all usernames in a csv file.

I managed to get hold of a old script, used by a former collegue of mine.

I will try this, but i dont know if it Works yet.
————————————————————————-

$users = Import-Csv -Delimiter ";" -Path "E:\CSV\HDrevLuk\OdenseBrugere.csv"
$no = $users.count

Write-host "Number of users in location: $no" -ForegroundColor Yellow

$correctOU = "OU=x,OU=01_x,DC=x,DC=it-x,DC=net"

$mydate = get-date -format yyyy-MM-dd_HH-mm
$logfile = new-item -path "E:\CSV\HDrevLuk\removeHomeDirectory$mydate.log"
add-content -Path $logfile "Log file for removal of HomeDirectory"

$oufile = new-item -path "E:\CSV\HDrevLuk\OUVer2.csv" -Force
add-content -path $oufile "Username;OU"

$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
}

}

March 13, 2017 at 1:36 pm

When you are removing values in AD you should use the -Clear switch to set that value to NULL. https://technet.microsoft.com/en-us/library/ee617215.aspx?f=255&MSPPError=-2147217396

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.

-Clear otherTelephone

March 13, 2017 at 1:38 pm

Hi Brian,

Thats a lot of code for removing one line from the users. If anything, I do not see the code to actualy remove the homefolder in your script.

Based on your info I would try:

$users = Import-Csv -Delimiter ";" -Path "E:\CSV\HDrevLuk\OdenseBrugere.csv"
 foreach ($user in $users)
{
Set-ADUser -Identity $user -HomeDirectory ''
}

March 13, 2017 at 1:41 pm

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:

Get-ADUser -Filter {SamAccountName -eq 'mytestaccount'} | Set-ADUser -Clear homeDirectory

then just update the filter for all users:

Get-ADUser -Filter * | Set-ADUser -Clear homeDirectory

March 13, 2017 at 1:41 pm

Hi Albert,
Yes i thought so.

As i can see, your example script does the following:

Sets the "home folder – local path" with no data in the textfield, instead of mapped drive, correct?

March 13, 2017 at 1:49 pm

Hi,

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"

?

March 13, 2017 at 2:06 pm

Nice, I did no notice the clear command.

Learned another thing today 🙂

March 14, 2017 at 12:04 pm

Hi Rob,

Thanks for the reply, but as i understand your code, it takes a "search" through AD, and changes the homedir.

What i really need, is a powershell that changes the Homefolder – Mapped H: drive,
to Local path "no data in field".

And it must read the users form a .csv .txt file or similar.

So i end up having a script, that change/sets the above from a username.txt list and thereby
can control what usernames has thier profiledata changed.

Best regards

March 15, 2017 at 10:56 am

Hi Again, Rob/Albert.

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.

Please see the mockup here: https://www.dropbox.com/s/i12tipngi78rap6/Change_Needed_Home_folder.jpg?dl=0

I am willing to pay a charge for help on this script, so i hope you/someone can help me on this.

Best regards