## Enumerate and backup profiles from remote Windows PCs using Robocopy
## Written by Jeff Harbert 3-30-2017
## This Powershell script is intended to be run once per day as a scheduled task
# Backups will go into folders named for days of the week.
$day = (get-date).dayofweek
# List of computers to get profiles from.
$computers = @("computer1","computer2","computer3")
foreach ($computerName in $computers)
{
# List of profiles will be saved to this file.
$saveFile = $computerName + "_profiles.txt"
# Grab the profiles form the remote PC and save the list to the file above.
gwmi -Namespace "root\cimv2" -Class win32_userprofile -Impersonation 3 -ComputerName $computerName | select LocalPath > $saveFile
# Start processing the file to isolate the user profile names.
$lines = (gc $saveFile)
foreach ($line in $lines)
{
$b = $line.contains("Users")
If ($b -eq "True")
{
$array = $line.Split("\")
$userName = $array[2]
# The output of the command above results in trailing whitespace. The next command eliminates it.
$userName = $userName.Trim()
# Specify the folders to be backed up
$folders = @("Desktop","Downloads","Documents","Music","Pictures")
foreach ($folder in $folders)
{
# This creates the Robocopy command that will be run for each folder listed above.
$cmd = '& robocopy /e /xo /nfl /r:5 /w:2 \\' + $computerName + '\c$\Users\' + $userName + '\' + $folder + ' f:\profiles\' + $day + '\' + $computerName + '\' + $userName + '\' + $folder + '\'
# Execute the Robocopy command.
Invoke-Expression $cmd
}
}
}
}
Comments