0

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

4
  • Quick thoughts: Start-Transcript $log or using try{}catch{} blocks, and/or perhaps using the -verbose switch. Commented Nov 30, 2023 at 12:18
  • you may try writing everything to the success stream: Copy-Item .... -Verbose *>&1 | Out-File .... Commented Nov 30, 2023 at 13:04
  • Thanks for the input but Start-Transcript or -Verbose do not format the output file is a user friendly pipe delimited way so I can see what copied from A to B with a "Success" or "Failure + Error Reason". I will keep playing to see if Powershell is even the best solution for this. Commented Nov 30, 2023 at 16:08
  • Might need some fancier PowerShell looping your copies and wrapping in a try{}catch{} block to capture success or failure (e.g. $_.Exception.Message) and listing $_.FullName for an idea of something else to play with. Commented Dec 4, 2023 at 12:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.