Search code examples
powershellloggingrobocopymirrormirroring

How to use robocopy mirror without output?


I am trying to delete a folder using robocopy mirroring like this: Start-Process -FilePath "robocopy.exe" -ArgumentList "$emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl" -Wait -PassThru -NoNewWindow but still get a line of output for every deleted file enter image description here

I tried adding >nul 2>&1 as explained in another answer here Start-Process -FilePath "robocopy.exe" -ArgumentList "$emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl >nul 2>&1" -Wait -PassThru -NoNewWindow but still get the same output.


Solution

  • Since you're running robocopy in the current console window (-NoNewWindow), synchronously (-Wait), there is no reason to use Start-Process at all - just invoke robocopy directly, which also allows you to use > redirections effectively:

    robocopy.exe $emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl *>$null
    

    Note:

    • Direct execution makes a program's stdout and stderr output directly available to PowerShell, via its success and error output streams.

    • *>$null is a convenient PowerShell shortcut for silencing all output streams - see about_Redirection.

    • Another benefit of direct invocation is that the external program's process exit code is reported in PowerShell's automatic $LASTEXITCODE variable.

    See also:


    As for what you tried:

    • You fundamentally cannot suppress output from a process launched with Start-Process -NoNewWindow on the PowerShell side.

    • Trying to silence command output at the source, i.e. as part of the target process' command line with >nul 2>&1, would only work if cmd.exe were the -FilePath argument and you passed a robocopy command to it. > redirections are a shell feature, and robocopy itself isn't a shell.