PowerShell - Robocopy

Hello, I’ve got no PowerShell experience, however I’ve got a situation where I need to make use of it to copy some folders.

$Source = Get-ChildItem “\ServerA\Backups\BU002\Backups” -recurse -exclude “STAGING_BACKUP_*” | Where-Object {$.PSIsContainer -eq $true -and $.Name -match “BACKUP”} $Dest = “\ServerB\Backups\BU002\Backups” robocopy $source $Dest /NP /E /W:10 /R:4 /X /V /Copy:DT /MAXAGE:2
How can I only copy the entire BACKUP folder and not just the contents within the folder, if that makes sense?

Deano, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code or error messages or sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”.
Thanks in advance.

That’s actually more a robocopy question than Powershell. :wink: robocopy takes as the source only a single folder path. You cannot provide an array. So if I got it right this should be enough:

$Source = ‘\ServerA\Backups\BU002\Backups\BACKUP’
$Dest = ‘\ServerB\Backups\BU002\Backups\Backup’
robocopy $source $Dest /NP /E /W:10 /R:4 /X /V /Copy:DT /MAXAGE:2

If that’s not what you want you will have to elaborate a little more detailed about your task.

Apologies, noted!

In my source location I would like to filter on a specific folder (“BACKUP”) and copy that folder to the destination and not just the contents within the folder if that makes sense.

 

What do you mean with filter? If you know the name (path) of the folder just provide this name (path).

That does make sense and I already showed you above how to achieve this. You have to include the name of the folder in the destination path you provide for robocopy.

Hi Dinko, a couple of things,

1 - Olaf showed you how to do it if you only have one back-up folder
2 - If you are expecting to get more than one folder returned, then you need to do a ForEach loop (ForEach ($Folder in $Source))
3 - Robocopy expects you to provide the full path, so you need to get the property “FullName” for the folder, so your first command could look like this:

$Source = Get-ChildItem "\\ServerA\Backups\BU002\Backups" -Recurse -Exclude "STAGING_BACKUP_*" -Include "BACKUP" -Directory | Select-Object -ExpandProperty FullName

4 - You only have a single slash in each UNC path instead of the expected \

Hope this helps.

Dinko just registered to the site.

Since i feel this is a bit related id like to see if you can review.

I cannot create a new thread waiting on the registration authorization. #waiting

ForEach ($system in (Get-Content C:\book1.txt)) {
# Retrieve a list of computers to connect to
$file = "\\$system\c$\CPI\Desktop\desktop.log"
#location of the file to retrieve
# Is the File there?
if ( Test-Path $file){
# Yup now make a copy of that file into local computer
copy "c:\CPI\Desktop\desktop.log" -Destination "\\my computer\c$\LETS\$system\"
}
}

`I believe the problem is on the copy #my intent is to copy from the $system ls to the -destination my local computer. on the if cmdlet can it pull the $system variable or does it “know” its within the $system ls/dir

Art, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code or error messages or sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

My apologies i hope it looks better. Thanks for the kind assistance to formatting.

You post probably is on hold for moderation. You probably editted too often in a short amount of time. The forum software (spam and malware detection) does not like that. :wink: