Migrating roaming profiles to Windows 7

Though a Microsoft user migration tool exists, I only want the bare minimum to be carried across from XP and I definitely want automation to ensure consistency. I made this script to be run by support technicians while logged in on newly installed Windows 7 as the user being migrated. The My Documents folder redirection is particularly important – the script mounts the old XP profile’s registry hive from the server, and checks whether My Documents was correctly redirected to the home drive. If not, the script will migrate the files from the old location. This tidies up any old inconsistent user profiles. Having My Documents on the home drive keeps profile size down for faster logins when roaming and helps to reduce the chance of a user losing data if their hard disk fails.

I spent a while wondering how to keep this as a single script despite the need to use RunAs for the Registry hive mount before hitting on the idea of recursion – see highlight below (%0 is the running script’s full name with path, %~nx0 is just the filename and extension). Despite the /env in the command which is supposed to make the invoked process share the main user environment, only some things are in fact shared. %TEMP% is for instance, though environment variables are not. So I pass the username as a command line, and recover the results of the Registry parse from a temporary file.

UPDATE – Since the file copying can take a long while I have made the My Documents path checking the first task, a failure of which will quit the script. There is now a five second delay between mounting the Registry hive and querying it) which should stop the script having to be run twice.

@echo off
set FServer=YourServerHere
echo .:: User profile migration script for user %USERNAME%

if not "%1" == "" goto checkxpreg

echo .:: We need admin rights to mount and check the user's XP profile registry hive
runas /env /noprofile /user:%USERDOMAIN%\Administrator "%~nx0 %USERNAME%"

if exist %TEMP%\XPMyDocsPath.txt type %TEMP%\XPMyDocsPath.txt
if not exist %TEMP%\XPMyDocsPath.txt (
  echo .:: ERROR the XP profile registry hive has not mounted - run the script again!
  goto :eof

if exist %TEMP%\DocsNeedMoving.txt (
  echo .:: Migrating content to H:\ ready for Windows 7
  xcopy /y /e /c "\\%FSERVER%\profiles$\%USERNAME%\My Documents\*.*" "H:\"
if not exist %TEMP%\DocsNeedMoving.txt (
  echo .:: No documents need moving

echo .:: My Documents redirection to root of H: drive
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal /t REG_SZ /d "H:" /f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal /t REG_EXPAND_SZ /d %%HOMEDRIVE%% /f

echo .:: IE Favorites
xcopy /y /e /c \\%FSERVER%\profiles$\%USERNAME%\Favorites\*.* %USERPROFILE%\Favorites\

echo .:: Outlook signatures
xcopy /y /e /c "\\%FSERVER%\profiles$\%USERNAME%\Application Data\Microsoft\Signatures\*.*" %USERPROFILE%\AppData\Roaming\Microsoft\Signatures\

echo .:: Outlook nicknames file
if not exist %USERPROFILE%\AppData\Roaming\Microsoft\Outlook\*.* md %USERPROFILE%\AppData\Roaming\Microsoft\Outlook
copy /y "\\%FSERVER%\profiles$\%USERNAME%\Application Data\Microsoft\Outlook\*.nk2" %USERPROFILE%\AppData\Roaming\Microsoft\Outlook\

echo .:: Word user templates
xcopy /y /e /c "\\%FSERVER%\profiles$\%USERNAME%\Application Data\Microsoft\Templates\*.*" %USERPROFILE%\AppData\Roaming\Microsoft\Templates\

echo .:: Desktop files and shortcuts
xcopy /y /e /c \\%FSERVER%\profiles$\%USERNAME%\Desktop\*.* %USERPROFILE%\Desktop\

:: Stop the shell from changing the display name of the folder to My Documents on the fileserver
if exist H:\Desktop.ini (
  attrib -r -s -h H:\Desktop.ini
  del H:\Desktop.ini
:: Cleanup
if exist %TEMP%\DocsNeedMoving.txt del %TEMP%\DocsNeedMoving.txt
del %TEMP%\XPMyDocsPath.txt

goto :eof

if exist del %%TEMP%\DocsNeedMoving.txt
reg load "HKEY_USERS\TempXPRegHive" "\\%FSERVER%\profiles$\%1\NTUSER.DAT"
echo .:: Waiting 5 seconds - Registry hive mount seems unreliable if queried immediately
ping -n 1 -w 5000 > nul
for /f "tokens=2* delims= " %%a in ('reg query "HKEY_USERS\TempXPRegHive\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal') do set TempMyDocs=%%b
echo XP profile My Documents=%TEMPMYDOCS%> %TEMP%\XPMyDocsPath.txt
echo %TEMPMYDOCS% | find /I "%1" && date /t> %TEMP%\DocsNeedMoving.txt
reg unload "HKEY_USERS\TempXPRegHive"
set TempMyDocs=

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s