# PowerShell & Robocopy

**URL:** <https://forums.powershell.org/t/powershell-robocopy/2754>\
**Category:** PowerShell Help\
**Created:** [June 26, 2014, 2:21am UTC](https://forums.powershell.org/t/powershell-robocopy/2754 "2014-06-26T02:21:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian-santillan](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@ian-santillan](https://forums.powershell.org/u/ian-santillan)\
**Post date:** [June 26, 2014, 2:21am UTC](https://forums.powershell.org/t/powershell-robocopy/2754/1 "2014-06-26T02:21:26Z")

</div>

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

:(☹

---

<div class="post-metadata">

**Author:** ![simon-wahlin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/simon-wahlin/32/407_2.png) [@simon-wahlin](https://forums.powershell.org/u/simon-wahlin)\
**Post date:** [June 26, 2014, 3:08am UTC](https://forums.powershell.org/t/powershell-robocopy/2754/2 "2014-06-26T03:08:54Z")

</div>

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\'
```

---

<div class="post-metadata">

**Author:** ![ian-santillan](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@ian-santillan](https://forums.powershell.org/u/ian-santillan)\
**Post date:** [June 26, 2014, 6:25am UTC](https://forums.powershell.org/t/powershell-robocopy/2754/3 "2014-06-26T06:25:31Z")

</div>

> [Simon Wåhlin wrote:](https://powershell.org/forums/topic/powershell-robocopy/#post-16553)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.

---

<div class="post-metadata">

**Author:** ![ian-santillan](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@ian-santillan](https://forums.powershell.org/u/ian-santillan)\
**Post date:** [June 26, 2014, 6:31am UTC](https://forums.powershell.org/t/powershell-robocopy/2754/4 "2014-06-26T06:31:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jhofferle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jhofferle/32/804_2.png) [@jhofferle](https://forums.powershell.org/u/jhofferle)\
**Post date:** [June 26, 2014, 10:45am UTC](https://forums.powershell.org/t/powershell-robocopy/2754/5 "2014-06-26T10:45:15Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![ian-santillan](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@ian-santillan](https://forums.powershell.org/u/ian-santillan)\
**Post date:** [June 27, 2014, 3:19am UTC](https://forums.powershell.org/t/powershell-robocopy/2754/6 "2014-06-27T03:19:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![javier-savinon](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@javier-savinon](https://forums.powershell.org/u/javier-savinon)\
**Post date:** [March 14, 2016, 3:32am UTC](https://forums.powershell.org/t/powershell-robocopy/2754/7 "2016-03-14T03:32:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/powershell-robocopy/2754/8 "2024-05-16T20:42:38Z")

</div>

Closed on May 16, 2024, 8:42pm UTC


