Copy-item = no output

Welcome Forums General PowerShell Q&A Copy-item = no output

Viewing 4 reply threads
  • Author
    Posts
    • #220749
      Participant
      Topics: 13
      Replies: 23
      Points: 172
      Rank: Participant

      Hello Fellow Powershellers

      I’ve been tasked with decommissioning a server.  This server has text files, CSV, XLS all over the C:\ drive.  I need to copy all of those files to a USB attached disk.  This is what I’ve come up with:

      
      $Source = “C:\”
      
      $Target = “F:\Import”
      
      $Include = ‘*.txt’,‘*.csv’,’*.xls’,’*.xlsx’
      
      Copy-Item -Path $Source -Destination $target -Force -Include $Include  -Recurse
      
      

      The folder F:\Import does exist.  Hopefully it’s something obvious I’m missing.

      Please help.

      Kind regards, Darren

    • #220752
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Why not using robocopy? It’ll be way faster anyway. 😉

      robocopy C:\ F:\import *.txt  *.csv *.xls *.xlsx /S 

      … should do it.

    • #220755
      Participant
      Topics: 13
      Replies: 23
      Points: 172
      Rank: Participant

      Thank you Olaf,

      and there was me trying to learn some PowerShell 😉  what was I thinking?

    • #220776
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      I’ve been tasked with decommissioning a server.

      Sorry. This didn’t sound like you just wanted to practice. 😉 … there are still situations where Powershell is not the number one option to choose.

      So – here the part for your education: 😉
      If you want to copy files you have to add an asterisk (*) at the end of your source directory

      $Source = 'C:\*'
      $Target = 'F:\Import'
      $Include = '*.txt', '*.csv', '*.xls', '*.xlsx'
      
      Copy-Item -Path $Source -Destination $target -Force -Include $Include

      But that wouldn’t copy files from subdirectories.
      If you want to include subdirectories you have to use a Get-ChildItem before … like this:

      Get-ChildItem -Path $Source -Include $Include -Recurse -Force | Copy-Item -Destination $Target -Recurse -Force
    • #220980
      Participant
      Topics: 13
      Replies: 23
      Points: 172
      Rank: Participant

      Hi Olaf,

      everyday is a school day in IT.  I try to use most situations to learn something.  It all adds to the spice of life.

      Thank you for coming back with further explanation.  It always seems so obvious when someone else demonstrates it.

      Kind regards, Darren

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