-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsers_Folder_Backup.ps1
More file actions
57 lines (46 loc) · 1.9 KB
/
Users_Folder_Backup.ps1
File metadata and controls
57 lines (46 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Users_Folder_Backup.ps1
# ---------------------------------------------
# This script is used for copying a Windows installs "Users" folder
# without file permission structures to make migrating easier.
# It excludes AppData folders and the whatever exclusions are defined below
# ---------------------------------------------
# Source Users folder to copy from (change as needed)
$source = "E:\Users"
# Destination folder for copying Users to (change as needed)
$destination = "H:\Users"
$excludeDirs = @()
# Check if source directory exists
if (!(Test-Path -Path $source)) {
Write-Host "Source directory $source does not exist. Exiting script." -ForegroundColor Red
pause
exit
}
# Add exclusion folders (change as needed)
$excludeDirs += "E:\Users\Default"
$excludeDirs += "E:\Users\Default User"
$excludeDirs += "E:\Users\All Users"
# Get all user directories in E:\Users
$userDirs = Get-ChildItem -Path $source -Directory
# Loop through each user directory and add its AppData directory to exclusions
foreach ($userDir in $userDirs) {
$appDataPath = Join-Path -Path $userDir.FullName -ChildPath "AppData"
if (Test-Path $appDataPath) {
$excludeDirs += $appDataPath
}
}
# Build the robocopy command with exclusions
$robocopyCommand = "robocopy `"$source`" `"$destination`" /E /COPY:DAT"
# Append the exclusion directories to the robocopy command
foreach ($dir in $excludeDirs) {
$robocopyCommand += " /XD `"$dir`""
}
# Print summary
Write-Host ("Found {0} user folders in source." -f $userDirs.Count) -ForegroundColor Cyan
Write-Host ("Total exclusions: {0}" -f $excludeDirs.Count) -ForegroundColor Yellow
Write-Host "Generated Command:" -ForegroundColor Green
Write-Host $robocopyCommand -ForegroundColor White
# Prompt to continue before executing the command
Read-Host "Press Enter to execute the above command, or Ctrl+C to cancel"
# Execute the command
Invoke-Expression $robocopyCommand
pause