search
top

How to Copy Files between Windows Systems with PowerShell Copy-Item

As a longtime Linux user I have always enjoyed the ease of being able to copy files between systems with scp, ssh and other means. With Windows systems this has been hit or miss in corporate environments with firewalls and block on the un-secure port 445 used by SMB. But with PowerShell there is another way for Windows administrators to enjoy what Linux / Unix admins have enjoyed for years.

The copy-item cmdlet gives this feature. This is the description according to Microsoft.

DESCRIPTION
The Copy-Item cmdlet copies an item from one location to another in a namespace. Copy-Item does not delete the items being copied. The particular items that the cmdlet can copy depend on the Windows PowerShell providers available. For example, when used with the FileSystem provider, it can copy files and directories and when used with the Registry provider, it can copy registry keys and entries.

Sounds great! So lets see how we put this in play for everyday use. Using Get-Help Copy-Item we see the syntax available:

Copy-Item [-LiteralPath] <string[]> [[-Destination] <string>] [-Container] [-Credential <PSCredential>] [-Exclude <string[]>] [-Filter <string>] [-Force] [-Include <string[]>] [-PassThru] [-Recurse] [-Confirm] [-WhatIf] [-UseTransaction] [<CommonParameters>]

First let’s say we want to copy a file on the system to another directory. Normally we can achieve this with the copy command, but let’s see how to do it with PowerShell. Open a PowerShell prompt and lets get started.

Copy-Item c:\logs\biglog.txt -destination c:\backup

So we just copied biglog.txt to c:\backup, pretty cool. So lets go a step further and copy a file from your system to a remote system.

copy-item .\clusnodeinfo.vbs -destination \\remoteserver\c$\support

There we go, we just completed a secure copy (scp) like in Linux but with a Windows system using PowerShell. The command we just executed copied clusnodeinfo.vbs to a remoteserver in the c:\support directory and used my credentials to authenticate. Now what this assumes is the file didn’t exist, if the file existed and we wanted to overwrite it, we would fail. But fear not we have the option to add -force to our command all is well.

copy-item .\clusnodeinfo.vbs -destination \\remoteserver\c$\support -force

So far we have just copied a file, how about a directory copy and it’s sub-directories? No problem. In  this example we’ll copy c:\support\dir1 and the sub-directories to remoteserver\dir1, we will force iot to overwrite and use -recurse to get the sub-directories.

 copy-item -path "c:\support\dir1" -destination "\\remoteserver\c$\dir1" -recurse -force

Alright, now we are cooking with gas! How about copy a file from one server to another from your workstation? Syntax is the same as before we just add the \\remoteserver\path with the file and we are good.

copy-item \\remoteserver\c$\support\clusnodeinfo.vbs -destination \\remoteserver2\c$\support

As for the directory copys we do the same.

copy-item -Path "\\remoteserver\c$\support\dir1" -destination "\\remoteserver2\c$\dir1" -recurse -force

Now you ask, what if I need to pass different credentials? Not an issue, we just pass -Credential “DOMAIN\id” option and we are good.

This just covers a portion of what is available with the copy-item cmdlet. Feel free to experiment with the other options!

 

4 Responses to “How to Copy Files between Windows Systems with PowerShell Copy-Item”

  1. Niveditha says:

    your code was helpful. can u please provide an example for copying files recursively over a folder from one server to another server with different credential for source and destination.

  2. Me says:

    Erm…
    Ancient post, but…
    You do realise that this is still using SMB/CIFS for the actual copying, right? It’s the same as any other SMB transfer, except you’re doing it from the PowerShell command line rather than a GUI.
    C$, D$ and so on are built in “administrative shares” that are enabled by default in all Windows versions since NT 4.0. It’s still an SMB share and requires SMB ports to be open and routed.

    • newlife007 says:

      Yes, I do realize and I know about the administrative shares, this was just of an example for command line using PowerShell that could be used in scripts. There are many tools and many ways to to many things in Windows, this is just an example. Ports and the SMB shares are a given in this example, There was no need at the time of the post to put that information along with the PowerShell example. Most competent Windows Administrators will already know this fact.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

top