How to use Powershell to batch copy files and output their success or failure to a log file

51 views Asked by At

I want to copy a series of files from different locations/destinations and output their success and/or failure to a pipe delimited file.

For example I have tried the code below but it doesn't report failures and I would also like the output to be clearer so I can consume this into excel and clearly see what copied and what failed.

Copy-Item -Path "C:\Source_1\111a46bf-cc88-48c1-b20a-cc8f897dec84.docx" -Destination "C:\Destination_1\Renamed_Word_Doc1.docx" -PassThru | Out-File -FilePath "C:\Output\copy_results.txt" -Append
Copy-Item -Path "C:\Source_2\222a46bf-cc88-48c1-b20a-cc8f897dec84.docx" -Destination "C:\Destination_1\Renamed_Word_Doc2.docx" -PassThru | Out-File -FilePath "C:\Output\copy_results.txt" -Append

Is there a way to do this so I have an output file that looks something the below, assuming the second file was to fail in the above example

Path "C:\Source_1\111a46bf-cc88-48c1-b20a-cc8f897dec84.docx" | -Destination "C:\Destination_1\Renamed_Word_Doc1.docx" | File copied successfully -Path "C:\Source_2\222a46bf-cc88-48c1-b20a-cc8f897dec84.docx" -Destination "C:\Destination_1\Renamed_Word_Doc2.docx" | Error Reason

For example I have tried the code below but it doesn't report failures and I would also like the output to be clearer so I can consume this into excel and clearly see what copied and what failed.

Copy-Item -Path "C:\Source_1\111a46bf-cc88-48c1-b20a-cc8f897dec84.docx" -Destination "C:\Destination_1\Renamed_Word_Doc1.docx" -PassThru | Out-File -FilePath "C:\Output\copy_results.txt" -Append
Copy-Item -Path "C:\Source_2\222a46bf-cc88-48c1-b20a-cc8f897dec84.docx" -Destination "C:\Destination_1\Renamed_Word_Doc2.docx" -PassThru | Out-File -FilePath "C:\Output\copy_results.txt" -Append

Is there a way to do this so I have an output file that looks something the below, assuming the second file was to fail in the above example

Path "C:\Source_1\111a46bf-cc88-48c1-b20a-cc8f897dec84.docx" | -Destination "C:\Destination_1\Renamed_Word_Doc1.docx" | File copied successfully -Path "C:\Source_2\222a46bf-cc88-48c1-b20a-cc8f897dec84.docx" -Destination "C:\Destination_1\Renamed_Word_Doc2.docx" | Error Reason

0

There are 0 answers