CMD batch file for user profile migration - windows

I am working on a refresh project, and I am rebuilding and swapping out a lot of computers. When doing a reimage, I have a script I can run which will copy the Desktop, Favorites, and Documents from all user profiles to a network share.
What I don't have yet, is a script for hardware replacements (vs a reimage) that will transfer data directly from the old computer to the new one. As with my other script, I need it to transfer data from the Desktop, Favorites, and Documents folders for all user directories present on the old machine. If I wanted to go a step further, I might exclude specific user profiles such as Public, Default, my own profile, etc, but a more basic script would still work.
I have done some batch scripting on my own, but I'm not sure what the syntax needs to be to have it cycle through all the user profiles on a remote machine.
Here is what I have tried so far:
#echo off
Set /p remotepc=Enter remote hostname:
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Desktop "C:\Users\%%~nxD\Desktop" /H /E /Y /K /I /R /C)
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Documents "C:\Users\%%~nxD\Documents" /H /E /Y /K /I /R /C)
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Favorites "C:\Users\%computername%\%%~nxD\Favorites" /H /E /Y /K /I /R /C)
pause

Related

Navigating to Desktop

I'm having trouble getting my bat file to work correctly. I run two hard drives on my computer and my desktop is on my E: drive. So %Userprofile%\Desktop navigates me to my desktop on E: , but when put into my bat file It tries to create another folder on my C: Drive called Desktop. Exactly the same spelling. But I will be using this on another computer That may or may not have their desktop on their C: drive.
I am copying a file to another folder
xcopy /s "%~dp0\Folder\Folder\Folder\Folder\File.exe" "%USERPROFILE%\Desktop\WorkFiles" /Y
This will attempt to create a Folder called Desktop and drop the exe into that.
The bat is ran on a USB hence the "%~dp0"
Your xcopy command has a trailing backslash included in %~dp0 so there is one too much. Replace with:
xcopy /s /Y "%~dp0Folder\Folder\Folder\Folder\File.exe" "%USERPROFILE%\Desktop\WorkFiles\"
If you relocated your Desktop from the normal position you have to lookup the actual location in the registry (or PowerShell/vbscript user shell folders)
If the Desktop is not relocated, this batch will nevertheless get the proper location:
Set "Key=HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Set "Val=Desktop"
For /F "Tokens=2*" %%A in (
'Reg Query "%Key%" /v "%Val%" 2^>Nul ^| Find "%Val%" '
) Do Set "Desktop=%%B"
Echo 'Desktop' location: %Desktop%
The better way to evaluate the Desktop on varying windows systems is to use eryksuns version:
#Echo off
for /f "usebackq delims=" %%a in (
`powershell -c "[environment]::GetFolderPath('Desktop')"`
) do set "desktop=%%a"

Speed Up Xcopy in Batch

So I'm trying to copy a backup of the data from my app. I wrote the batch script below to do this, but the script takes forever to run.
I start the batch script at 1am, and it is still running at 8:30am. This seems weird to me because when I copy the backup of my app manually in Windows File Explorer, it copies in 7-15 minutes depending on network traffic.
I REM the %backupcmd% "C:\Program Files\App\App Server\Data\Backups" "%drive%\" line. That was the original line of batch script I used to backup the data, and it worked efficiently up till a month ago.
So I tried the xcopy command with /d, so it would only copy source files that have been changed on or after that date (the current date), and the backups I'm copying are made at 12:01am every night and the copy backup script starts at 1am.
Any advice as to how to speed up my xcopy would be appreciated. If you think I should use powershell for this task too, I'm open to that option as well.
#echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set yyyy=%ldt:~0,4%
set mm=%ldt:~4,2%
set dd=%ldt:~6,2%
:: variables
set drive=Z:\RootSoft\App\Data Backups
set backupcmd=xcopy /s /c /d /e /h /i /r /y /f /z
echo ### Backing up Backup...
REM %backupcmd% "C:\Program Files\App\App Server\Data\Backups" "%drive%\"
xcopy "C:\Program Files\App\App Server\Data\Backups" "Z:\RootSoft\App\Data Backups" /D:%mm%-%dd%-%yyyy% /s /c /e /h /i /r /y /f /z
:: use below syntax to backup other directories...
:: %backupcmd% "...source directory..." "%drive%\...destination dir..."
echo Backup Complete!
echo %errorlevel%
pause
You could try with ROBOCOPY and /MT switch which could accelerate the copy.
Also you can make some test by measuring the during process with TimeThis that can be found here (no need to be installed, just extract the exe with 7z in the current batch file folder)
netsh interface tcp show global
netsh int tcp set heuristics disabled
netsh int tcp set global autotuninglevel=disabled
netsh int ip set global taskoffload=disabled

Why isn't this xcopy command not working?

I am trying to run a simple xcopy batch script but I am facing some difficoulties.
Here is the script
SET SRC=C:\FOLDER1\FOLDER2
SET DEST=V:\FOLDER1
FOR /D %%d in (%SRC%\*) do xcopy /S /I /y /exclude:%SRC%\exclude.txt %%d V:\FOLDER1\%%~nxd
Basically this script should copy some of the subfolders of C:\FOLDER1\FOLDER2 (excluded.txt contains a list of directories to exclude) and its content to the destination. However when I run the scripts, altough no errors are thrown, NO FILES OR FOLDERS get copied. What am I doing wrong?
Interestingly if I run the following script INSIDE FOLDER2, everything procedes as expected.
FOR /D %%A in (*) DO xcopy /S /I /y /exclude:exclude.txt %%A V:\FOLDER1\%%A
xcopy "C:\FOLDER1\FOLDER2" "V:\FOLDER1" /e /i /y
You mention nothing about excluding any folders.

Remove specific directory from multiple users in Windows

I'm trying to remove a specific directory from every User's AppData directory within Win7. We had a bad install of Cisco on multiple computers, and part of fixing it is that we need to remove AppData\Local\Cisco from every user on the computer prior to uninstalling, Wouldn't be an issue except every computer has anywhere from 5-20 users on it. I've tried the following:
for /D %%f in (Cisco) do rmdir %%f /s
And running that from the Users directory. It's not working though, and I'm unsure the best route to removing this directory from all users on the computer, if I try a del or rmdir with wildcards, that breaks as well. Any ideas?
Run this in the users folder from the cmd prompt and check that it returns the correct folders.
for /d /r %a in (cisc*) do #echo %a
If so then add the RD command.
for /d /r %a in (cisc*) do rd /q /s "%a"

Copying User Profile to Default Profile in Windows 7

I'm trying to find a way using, hopefully, a batch file to copy a modified user profile to the default user profile so that any user that logs in to the computer will adopt the profile setup. We're using a utility called pGina to call the default profile for a user logged in through a RADIUS server, as opposed to having the user log in to the domain directly. Currently, we're trying to use robocopy for this.
cd C:\Users\
rmdir Default /s /q
robocopy /COPY:DAT /R:5 /E C:\Users\user1 C:\Users\Default
pause
Does anyone have any idea how to successfully copy the profile? Thanks in advance.
This is what I found when searching for an answer to your problem
rem delete old Default User profile
RD /s /q "%systemdrive%\Profiles\Default User"
rem copy current user profile to default user profile
xcopy "%USERPROFILE%\*.*" "%systemdrive%\Profiles\Default User" /e /c /I /H /R /K /Y
rem delete non-need some files
del /f /q "%systemdrive%\Profiles\Default User\*.*"
rem set default attributes
attrib +h "%systemdrive%\Profiles\Default User"
rem registry trick
rem no directly copy locked ntuser.dat file
rem use reg tools to save current user registry to file
reg save HKCU "%systemdrive%\Profiles\Default User\ntuser.dat"
rem set default attributes to hive file
attrib +H +S "%systemdrive%\Profiles\Default User\ntuser.dat"

Resources