- This topic has 4 replies, 2 voices, and was last updated 1 month, 1 week ago by
April 20, 2020 at 3:31 pm #220749
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.
Kind regards, Darren
April 20, 2020 at 3:46 pm #220752ParticipantTopics: 4Replies: 2247Points: 5,484Rank: Community MVP
Why not using robocopy? It’ll be way faster anyway. 😉
robocopy C:\ F:\import *.txt *.csv *.xls *.xlsx /S
… should do it.
April 20, 2020 at 3:59 pm #220755
Thank you Olaf,
and there was me trying to learn some PowerShell 😉 what was I thinking?
April 20, 2020 at 4:40 pm #220776ParticipantTopics: 4Replies: 2247Points: 5,484Rank: 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
April 21, 2020 at 7:53 am #220980
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
- You must be logged in to reply to this topic.