Script to Search Netlogon logon scripts and Replace Drive Mappings


Ace Fekay here again!

Once again, As many of you know, I’m an avid Active Directory and Exchange server engineer/architect, and an MVP in Active Directory. And why am I posting simple stuff, you ask. Well, because we need to use this stuff day to day, that’s why.

Yea, this may be simple, but you’d be surprised who may struggle with it, like I did. I had to get help from a colleague who put the bulk of this together. I first had an idea with my beginner’s mentality to do it a little differently, but when I saw what he suggested, I said, hmm, I still have lots to learn.

I hope this, and my future scripts, especially with Office 365, help you out.


After migrating shares from one server to another server using a Robocopy script (that I’ll post later), we needed to change the drive mappings in the logon scripts in the Netlogon share.

Keep in mind, we already have a robust DFS in place. The new sharename has targets to the old server. However, we needed to change any logon scripts still referencing the old server by either NetBIOS or by FQDN (, with 28,000 scripts, that’s something we’re not going to do manually.

This script replaces any mappings using the old server name, “OldServer” such as either \\olderserver\sharename or \\\sharename, to the new DFS name, \\\NewShareName.


This works fine. Watch the word-wrap in the blog.

# First run the robocopy script to copy all data
# Then run the netlogon report script to see how many bat files in netlogon reference OldServer
# Then run this script to replace any reference to “OldServer” to the new DFS sharename in the batch files for each share.
# By Ace Fekay and a colleague, who put together the bulk of this together.
# I added counters and report to the screen.
# If you need to run it as a different users, un-remark the following
# get-credential

$Path = “\\\NETLOGON\”
$FilesAltered = 0
$FilesProcessed = 0
# This code snippet gets all the files in $Path that end in “.bat”.
cd $Path

Get-ChildItem -Filter “*.bat” | foreach{
$file = Get-Content $_
    #only modify files that contain the string “OldServer”
    if (Select-String -InputObject $file “OldServer”){
    $file = Get-Content $_
    $file = $file -replace “\\\\OldServer\\Users”,”\\\\OldServer-Users”
    $file = $file -replace “\\\\\\users”,”\\\OldServer-User”
    $file = $file -replace “\\\\OldServer\\Department”,”\\\\OldServer-Department”
    $file = $file -replace “\\\\\\Department”,”\\\OldServer-Departmentt”
    $file = $file -replace “\\\\OldServer\\GDrive”,”\\\OldServer-GDrive”
    $file = $file -replace “\\\\OldServer\\FDrive”,”\\\OldServer-FDrive”
    $file = $file -replace “\\\\OldServer\\HDrive”,”\\\OldServer-HDrive”
    $file = $file -replace “\\\\OldServer\\Share2\$”,”\\\OldServer-Share2$”
#comment out any net time statements, if they exist
    $file = $file -replace “^net time”,”REM net time”
#write out the changes
    Set-Content -Value $file -Path $_;
    Write-Host $_.Name
    write-host $file
    Write-Host “”
Write-Host $FilesAltered ” altered out of a total of” $FilesProcessed “files processed.” 

Comments are welcomed.



I hope this helps!

Published 9/9/2015

Ace Fekay
MVP, MCT, MCSE 2012, MCITP EA & MCTS Windows 2008/R2, Exchange 2013, 2010 EA & 2007, MCSE & MCSA 2003/2000, MCSA Messaging 2003
Microsoft Certified Trainer
Microsoft MVP – Directory Services

clip_image002622 clip_image004622 clip_image006622 clip_image008622 clip_image010622 clip_image012622 clip_image014622

Complete List of Technical Blogs:

This posting is provided AS-IS with no warranties or guarantees and confers no rights.