this post was submitted on 12 May 2025
8 points (100.0% liked)

Powershell

1124 readers
2 users here now

PowerShell (POSH) is a a task automation command-line shell and scripting language created by Microsoft. It became part of the FOSS community in 2016 and is now available across Windows, Linux, and macOS

Resources:


Rules:

Self-promotion rules:


founded 2 years ago
MODERATORS
 

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.sharepointdiary.com/2022/03/powershell-wait-for-command-to-finish.html#h-piping-to-out-null-to-wait-for-the-operation-to-complete

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

top 4 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 6 days ago* (last edited 6 days ago) (1 children)

I'm not at all computer, but I believe Out-Null is used as "don't give me any output", Out-File is used as "put the output in this file", and Out-Host is similar to the standard output in the terminal.

What I would do is something like this (I'm not at a computer to test this):

Copy-Item "$SourceRoot\$SourceFolder\$SourceFile" -Destination "$DestinationRoot\$DestinationFolder" -Verbose *>&1 | Out-File -FilePath "$LogFile" -Append ; if(Test-Path "$DestinationRoot\$DestinationFolder\$SourceFile"){ Write-Host "Success!" } else { Write-Host "Failed!!!" }

Or if you want the actual output, this will store the result to the file and the screen/host:

Copy-Item "$SourceRoot\$SourceFolder\$SourceFile" -Destination "$DestinationRoot\$DestinationFolder" *>&1 | Tee-Object -FilePath "$LogFile" -Append | Out-Host
[–] ZTechnical 1 points 6 days ago (1 children)

Thanks for the reply.

I think your first option can give a false positive if the file is started to exist but hasn’t been completely written to. (Except if Out-File already implies that it’s done being written, so the next part isn’t really needed.)

The second one is good, but I don’t need console output. Just logging.

I’m looking for confirmation that both Out-Null and Out-File have the same (side) effect of making sure the file is done being copied before continuing to the next line of the script. If so, I’ll stick with Out-File

[–] [email protected] 2 points 6 days ago (1 children)

Oh my bad, I misunderstood your question initially. Yes, Out-File will wait for the previous command to finish just like Out-Null.

[–] ZTechnical 1 points 6 days ago