HOWTO · PowerShell

PowerShell With Robocopy and Arguments Passing

Learn how to copy files and directories with robocopy from PowerShell, including how to pass options as separate arguments.

On this page

To copy files or directories from PowerShell, call the Windows robocopy utility with a source directory, a destination directory, and any file selection or options you need. The command pattern below shows how to place those arguments; the PowerShell example demonstrates how to keep multiple options separate.

Use the robocopy Command to Copy Files in PowerShell

The command pattern is robocopy <Source> <Destination> [<File(s)>] [<Options>]. The source and destination are directories. The optional file selection can name one or more files or use a pattern, while options control the copy behavior. Robocopy is a Windows command-line utility, so run the command on Windows with robocopy.exe available.

For example, this command copies report.txt from one directory to another and suppresses Robocopy’s job header and job summary:

robocopy C:\Work\Source C:\Work\Backup report.txt /njh /njs

Replace the sample paths and filename with locations that exist on your computer. To copy a directory tree, omit the file selection argument and place options after the destination. Robocopy copies files in the specified directories; use options such as /S or /E to include subdirectories according to the behavior you need. Confirm the expected files arrived and review Robocopy’s report before relying on the copy for a backup or synchronization task.

This PowerShell example keeps the options in separate array elements and displays the list that can be splatted into a command call:

$options = @('/njh', '/njs')
"Options passed: $($options -join ', ')"
Options passed: /njh, /njs

When you invoke an external program in PowerShell, splatting an array passes each element as its own argument. For example, after setting $source, $destination, and $file to the intended paths and filename, call & robocopy $source $destination $file @options. The call itself requires Windows and Robocopy, so it is shown inline rather than as part of the portable array example.

Avoid storing multiple options in one string such as '/njh /njs': PowerShell passes that value as a single argument. Splitting a simple string on spaces can work for simple switches, but it is not a general argument parser. It can break when a value contains spaces or uses quoting, because whitespace alone does not describe how an argument should be grouped. Prefer an array with one complete argument per element.

You can add more switches as separate array elements, or use a pattern such as *.txt for file selection. Check the copied files and interpret Robocopy’s reported result using its documented exit-code meanings; values below 8 indicate that no copy failure was reported, while 8 or higher indicates at least one failure. For option details and exit-code meanings, see Microsoft’s Robocopy command reference.

Conclusion

Use robocopy from PowerShell with source and destination directories, followed by an optional file selection and options. Keep multiple options in an array and splat it so each reaches Robocopy as a separate argument. Verify the copied files and check Robocopy’s reported result.