PowerShell – Robocopy

Welcome Forums General PowerShell Q&A PowerShell – Robocopy

  • This topic has 8 replies, 4 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 8 reply threads
  • Author
    Posts
    • #221679
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      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?

    • #221700
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      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. 😉 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.

    • #221751
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      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.

       

    • #221757
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      In my source location I would like to filter on a specific folder (“BACKUP”)

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

      and copy that folder to the destination and not just the contents within the folder if that makes sense.

      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.

    • #221862
      Participant
      Topics: 10
      Replies: 27
      Points: 188
      Helping Hand
      Rank: Participant

      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.

    • #221868
      Participant
      Topics: 0
      Replies: 2
      Points: -11
      Rank: Member

      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

      • This reply was modified 1 month ago by cyrut247.
      • This reply was modified 1 month ago by cyrut247.
      • This reply was modified 1 month ago by cyrut247.
    • #221874
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      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.

    • #221880
      Participant
      Topics: 0
      Replies: 2
      Points: -11
      Rank: Member

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

    • #221886
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      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. 😉

Viewing 8 reply threads
  • You must be logged in to reply to this topic.