PowerShell & Robocopy

I’m writing a robocopy script in PowerShell to backup everything in C:\ excluding the directories and files in excludedfiles.txt and excludeddirs.txt.

$username = [Environment]::UserName
$src = Get-Content '\The BUS\src.txt'
$dest = Get-Content '\The BUS\dest.txt'
$excludedfiles = Get-Content '\The BUS\excludedfiles.txt'
$excludedirs = Get-Content '\The BUS\excludedirs.txt'

Robocopy.exe $src $dest /MIR /IS /XD $excludedirs /XF $excludedfiles /R:3 /W:3 /NP /L /LOG+:"\The BUS\log\log.txt" /A-:SH /TEE
Copy-Item '\The BUS\files\*' '\The BUS\'

I’m having issues with getting powershell to read a variable from the excludeddir.txt file.

The textfile excludeddir.txt includes

C:\The BUS
C:\inetpub
C:\Intel
C:\LOGS
C:\MSOCache
C:\PerfLogs
C:\Program Files
C:\Program Files (x86)
C:\ProgramData
C:\Temp
C:\boot
C:\Recovery
C:\Windows
C:\System Volume Information
C:\Users$username\AppData\Local\Application Data
C:\Users$username\AppData\Local\Microsoft\Internet Explorer
C:\Users$username\AppData\Local\Microsoft\Windows
C:\Users$username\AppData\Local\Temporary Internet Files
C:\Users$username\AppData\Local\ElevatedDiagnostics
C:\Users$username\AppData\Local\Temp
C:\Users$username\AppData\Local\History
C:\Users$username\Cookies
C:\Users$username\Local Settings
C:\Users$username\Net Hood
C:\Users$username\PrintHood
C:\Users$username\SendTo
C:\Users$username\Templates
C:\Users\admaint
C:\Users\Administrator
C:\Users\ADMINI~1
C:\Users\Default
C:\Users\Default User
C:\Users\DefaultAppPool
C:\Users\Public
C:\Users\All Users
C:\Users\esantillan
C:\Users\isantillan.old
C:\Users\sccm_content_manager
C:$Recycle.Bin

:(:frowning:

Robocopy needs each folder that contains a space to enclosed in quotes and to run an exe-file from powershell each argument should be a string.

Try something like this (Altho I haven’t been able to test it so maybe you should add /L to only do the logging and no actual copying):

$username = [Environment]::UserName
$src = Get-Content '\The BUS\src.txt'
$dest = Get-Content '\The BUS\dest.txt'
$excludedfiles = "`"$((Get-Content 'C:\temp\excludedfiles.txt') -join '" "')`""
$excludedirs = "`"$((Get-Content 'C:\temp\excludeddirs.txt') -join '" "')`""
 
robocopy.exe $src $dest "/MIR" "/IS" "/XD" $excludedirs "/XF" $excludedfiles "/R:3" "/W:3" "/NP" "/L" "/LOG+:`"\The BUS\log\log.txt`"" "/A-:SH" "/TEE"
Copy-Item '\The BUS\files\*' '\The BUS\'
Simon Wåhlin wrote:Robocopy needs each folder that contains a space to enclosed in quotes and to run an exe-file from powershell each argument should be a string.

Try something like this (Altho I haven’t been able to test it so maybe you should add /L to only do the logging and no actual copying):

$username = [Environment]::UserName
$src = Get-Content '\The BUS\src.txt'
$dest = Get-Content '\The BUS\dest.txt'
$excludedfiles = "`"$((Get-Content 'C:\temp\excludedfiles.txt') -join '" "')`""
$excludedirs = "`"$((Get-Content 'C:\temp\excludeddirs.txt') -join '" "')`""
 
robocopy.exe $src $dest "/MIR" "/IS" "/XD" $excludedirs "/XF" $excludedfiles "/R:3" "/W:3" "/NP" "/L" "/LOG+:`"\The BUS\log\log.txt`"" "/A-:SH" "/TEE"
Copy-Item '\The BUS\files\*' '\The BUS\'

Hello Simon, all that gives me is what i have in the text file in quotes. It’s still reading the $username variable as text and not as a variable that gives me the current username.

How do i make PowerShell recognize that $username in the text file (excludeddirs.txt) is a variable and not a string?


ROBOCOPY :: Robust File Copy for Windows

Started : Thursday, June 26, 2014 2:05:26 PM
Source : C:
Dest : C:\The BUS\dest\

Files : *.*

Exc Files : Config.Msi
NTUSER.DAT
UsrClass.dat
*.sys
*.content
*.LOG1
*.LOG2

Exc Dirs : C:\The BUS
C:\inetpub
C:\Intel
C:\LOGS
C:\MSOCache
C:\PerfLogs
C:\Program Files
C:\Program Files (x86)
C:\ProgramData
C:\Temp
C:\boot
C:\Recovery
C:\Windows
C:\System Volume Information
C:\Users$username\AppData\Local\Application Data
C:\Users$username\AppData\Local\Microsoft\Internet Explorer
C:\Users$username\AppData\Local\Microsoft\Windows
C:\Users$username\AppData\Local\Temporary Internet Files
C:\Users$username\AppData\Local\ElevatedDiagnostics
C:\Users$username\AppData\Local\Temp
C:\Users$username\AppData\Local\History
C:\Users$username\Cookies
C:\Users$username\Local Settings
C:\Users$username\Net Hood
C:\Users$username\PrintHood
C:\Users$username\SendTo
C:\Users$username\Templates
C:\Users\admaint
C:\Users\Administrator
C:\Users\ADMINI~1
C:\Users\Default
C:\Users\Default User
C:\Users\DefaultAppPool
C:\Users\Public
C:\Users\All Users
C:\Users\esantillan
C:\Users\isantillan.old
C:\Users\sccm_content_manager
C:$Recycle.Bin

Options : . /L /TEE /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /NP /IS /A-:SH /R:3 /W:3

Get-Content won’t perform variable expansion, so you could try something like this:

$excludedFiles = gc .\excludedfiles.txt | % { $_ -replace ‘$Username’, “$username”}

I was able to get it working by replacing the $username within excludeddirs.txt to “zzz” and then replacing the “zzz” with $env:USERNAME after calling Get-Content ‘\The BUS\excludeddirs.txt’

$excludeddirs = $excludeddirs -replace "zzz", $env:USERNAME

thanks everyone!

/ENDTHREAD

Hi, i need your help to run PS Script that should do a backup using robocopy options but is not, basically i need that the script create a FOLDER named with the date of the current day, then i need that robocopy run with some options, please see my code and tell me what im doing wrong:
$Date=(Get-date -Format d).Replace("/","-")
robocopy “D:\Test” “F:\BK_Server$Date” /S /E /COPYALL /V /ETA /R:10 /W:30 /LOG:“F:\BK_Server\log-$date.txt”

Thanks a lot.