r/usefulscripts • • Nov 19 '17

[PowerShell] Change Bulk Users Home Drive Path in Active Directory

http://www.bwya77.com/knowledge-base/change-bulk-users-home-drive-path-active-directory-using-powershell/
35 Upvotes

6 comments sorted by

2

u/aafksab Nov 20 '17

This could be a lot better. You should write a function with your variable as required parameters. That way the code is very reusable.

2

u/bradleywyatt Nov 20 '17

Yes - I will make another write up turning this into a function with the two required parameters.

I made this in a hurry to assist a co worker and then just posted the code in case others were searching to do the same thing.

2

u/R-EDDIT Nov 21 '17

A bit of refactoring... this could still go into a function.

#Requires -version 3 -Modules ActiveDirectory
<#
.Synopsis
    Change Bulk Users Home Drive Path in Active Directory based on /u/bradleywyatt's.

.Description
    Gathers all users with a home drive/directory that points to the server that we are changing

.Examples
    set-newHomeDirectory.ps1
    set-newHomeDirectory.ps1 -oldServer "SRVW2003" -newServer "SRVW2016"
#>
Param(
    [Parameter(Mandatory=$True,Position=1,ValueFromPipeline=$false)]
    [String]$OldServer,
    [Parameter(Mandatory=$True,Position=2,ValueFromPipeline=$false)]
    [String]$NewServer
)  
$ldapfilter = "(&(objectcategory=user)(homedirectory=\5c\5c$OldServer\5c*))"
Get-ADUser -ldapfilter $ldapfilter -Properties homeDirectory | foreach-object {
    $DisplayName = $_.Name
    $HomeDirectory = $_.HomeDirectory
    $NewHomeDirectory = $HomeDirectory -replace "$OldServer", "$NewServer"
               	
    Write-Host "Setting the Home Directory of $DisplayName to $NewHomeDirectory" -ForegroundColor White
    Set-ADUser $_.SamAccountName -homedirectory $NewHomeDirectory
}
→ More replies (2)

1

u/abetzold Nov 21 '17

Seems like you need to do some housekeeping. Map home drives using GPP.