Copy-item = no output

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:

[pre]

$Source = "C:"

$Target = “F:\Import”

$Include = ‘.txt’,'.csv’,‘.xls’,'.xlsx’

Copy-Item -Path $Source -Destination $target -Force -Include $Include -Recurse

[/pre]

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

Please help.

Kind regards, Darren

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

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

… should do it.

Thank you Olaf,

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

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

So - here the part for your education: :wink:
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

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