I have a script where I copy a file using this code:
Copy-Item "$SourceRoot\$SourceFolder\$SourceFile" -Destination "$DestinationRoot\$DestinationFolder" -Verbose *>&1 | Out-File -FilePath "$LogFile" -Append
I want to make sure the file being copied is done being copied, and I have seen that one way to do this is to use Out-Null
. I’m wondering if Out-File
can be used instead. That would be helpful since that’s what I’m already using. I seem to be seeing that this is true, but I would like assurance that this is indeed true.
Secondary question: Does redirecting all streams make any difference?
Here are some pages referring to using Out-Null
to make sure the command has completed:
https://theitbros.com/wait-for-a-command-powershell/
This page seems to indicate that Out-File
should have the same effect: “The Out-Null cmdlet is used when you don’t care about the output of an external command. To redirect the command output, you can use Out-File or Out-Host instead.”
https://www.reddit.com/r/PowerShell/comments/5c1jpn/waiting_for_a_cmdlet_to_finish_before_moving_on/
https://www.delftstack.com/howto/powershell/wait-for-each-command-to-finish-in-powershell/#using-the-out-commands-to-wait-for-each-command-to-finish-in-powershell
“Other examples of Out commands that we can use are the following: … Out-File …”
https://stackoverflow.com/questions/12211131/want-to-wait-till-copy-complete-using-powershell-copy-item
references:
https://www.itprotoday.com/powershell/forcing-powershell-to-wait-for-a-process-to-complete
awesome