Thursday, December 20, 2018

Bulk changing home directories through powershell

I do a lot of file server migrations and these two commands come in super handy. The first lists the home directories and such for all of the users on the domain.

Get-ADUser -filter * -properties scriptpath, homedrive, homedirectory | ft samaccountname, homedrive, homedirectory

And now for the real meat. You moved all of the data with robocopy and now need to point everything to the new location.

Get-ADUser -filter * -properties scriptpath, homedrive, homedirectory | foreach-object { $sam = $_.SamAccountName; if ($_.homedirectory) {set-aduser -identity $_ -homedirectory "\\<new server>\<subfolder>\$sam"} }

I made this post for my own future reference, but I hope this helps someone else.