Scripted Folder Redirection

This is how to script folder redirection—to be used in Windows 7 post-setup (right after imaging a machine)

This will pull account information from a text file, generate a user profile based upon that information, and then redirect that user profile – even before the user actually logs in for the first time.

@echo off
setlocal enableextensions enabledelayedexpansion
color 0a

set FName=
set LName=
set COMB=
set PWord=
set count=0

set count=1

rem policy – used to guarantee the redirect works properly – the user_account.txt is required
if not exist user_account.txt exit /b 0

rem pulls first name from account file
for /f “delims=” %%b in (user_account.txt) do (
if !count!==1 set FNAME=%%b
Set FName=!FNAME:~0,1!
set /a count+=1
)

rem pulls last name from account file
set count=1
for /f “delims=” %%c in (user_account.txt) do (
if !count!==2 set LNAME=%%c
set /a count+=1
)

rem sets username based upon first initial and lastname from account file
set COMB=!FNAME!!LNAME!

rem sets password from account file
set count=1
for /f “delims=” %%d in (user_account.txt) do (
if !count!==3 set PWord=%%d
set /a count+=1
)

rem generates user profile to be used to set folder redirection
C:\setup\scripts\cpau.exe -u %computername%\%COMB% -p %PWord% -ex “cmd.exe /c” -lwp -wait -hide
ping -n 10 127.0.0.1>nul

rem creates folder structure on d drive using username
if not exist D:\Users\%COMB% md D:\Users\%COMB%

rem copies c drive profile to d drive
robocopy /copyall /mir /xj /r:1 /w:0 C:\Users\%COMB% D:\Users\%COMB%

rem deletes c drive profile
if exist D:\Users\%COMB% rmdir /s /q C:\Users\%COMB%

rem creates junction from c drive profile to d drive profile
if exist D:\Users\%COMB% mklink /J C:\Users\%COMB% D:\Users\%COMB%


UPDATED: Some other stuff I noticed (4/7/2014):

rem removes the lock overlay that appears after redirection
reg.exe delete “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\SharingPrivate” /f

REM THIS IS USED TO FIX THE PROFILE PATH IN THE REGISTRY. CHANGED FROM C DRIVE TO D DRIVE.
REM OTHERWISE A TEMP PROFILE MAY BE LOADED – WHICH ISN’T GOOD
set count=1
set REGKEY=

FOR /F “tokens=*” %%a IN (‘REG.EXE QUERY “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList”‘) DO (
if !count!==9 set REGKEY=%%a
set /a count+=1
)
reg.exe delete “%REGKEY%” /v ProfileImagePath /f
ping -n 4 127.0.0.1>nul
reg.exe add “%REGKEY%” /v ProfileImagePath /t REG_SZ /d D:\Users\%COMB% /f

email me