Copy file remotely using PowerShell

I am writing a PowerShell script that I want to run from server A. I want to connect to server B and copy the file to server A as a backup.

If this is not possible, I would like to connect to server B from server A and copy the file to another directory on server B.

I see the Copy-Item command, but I don’t see how to give it a computer name.

I would think that I could do something like

 Copy-Item -ComputerName ServerB -Path C:\Programs\temp\test.txt -Destination (not sure how it would know to use ServerB or ServerA) 

How can i do this?

+79
powershell
May 24 '12 at 16:30
source share
5 answers

Just use administrative resources to copy files between systems. So much easier.

 Copy-Item -Path \\serverb\c$\programs\temp\test.txt -Destination \\servera\c$\programs\temp\test.txt; 

Using UNC paths instead of local file systems, you can help ensure that your script is executed from any client system that has access to these UNC paths. If you use local file system paths, then you run the script on a specific computer.

This only works when a PowerShell session is running under a user who has rights to both administrative resources.

I suggest using a regular network resource on server B with read-only access for everyone and just call (from server A):

 Copy-Item -Path "\\\ServerB\SharedPathToSourceFile" -Destination "$Env:USERPROFILE" -Force -PassThru -Verbose 
+82
May 24 '12 at 18:48
source share

Starting with version 5 PowerShell (included with Windows Server 2016, loaded as part of WMF 5 for earlier versions ), this is possible with remote interaction. The advantage of this is that it works, even if for some reason you cannot access shared resources.

For this to work, PowerShell 5 or higher must be installed in the local session where the copying starts. You do not need to install PowerShell 5 for a remote session β€” it works with PowerShell versions 2 and Windows Server versions starting from 2008 R2. [one]

From server A, create a session on server B:

 $b = New-PSSession B 

And then from A:

 Copy-Item -FromSession $b C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt 

Copying elements to B is done using -ToSession . Note that local paths are used in both cases; You must keep track of which server you are on.




[1]: when copying from or to a remote server with only PowerShell 2 installed, beware of this error in PowerShell 5.1 , which at the time of writing means that recursive copying of files does not work with -ToSession , apparently copying doesn’t at all I work with -FromSession .

+63
Aug 24 '16 at 11:48
source share

Use net use or New-PSDrive to create a new drive:

New-PsDrive: Create a new PsDrive visible only in PowerShell:

 New-PSDrive -Name Y -PSProvider filesystem -Root \\ServerName\Share Copy-Item BigFile Y:\BigFileCopy 

Network Usage: Create a new drive visible in all parts of the OS.

 Net use y: \\ServerName\Share Copy-Item BigFile Y:\BigFileCopy 
+36
May 24 '12 at 17:11
source share

Just in case, if you need your credentials to access the remote file, you can generate a System.Net.WebClient object using the New-Object cmdlet for Remote File Copy, for example, like this:

 $Source = "\\192.168.xx\somefile.txt" $Dest = "C:\Users\user\somefile.txt" $Username = "username" $Password = "password" $WebClient = New-Object System.Net.WebClient $WebClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) $WebClient.DownloadFile($Source, $Dest) 

Or, if you need to upload a file, you can use UploadFile:

 $Dest = "\\192.168.xx\somefile.txt" $Source = "C:\Users\user\somefile.txt" $WebClient.UploadFile($Dest, $Source) 
+13
Jun 20 '14 at 4:59
source share

None of the above answers worked for me. I kept getting this error:

 Copy-Item : Access is denied + CategoryInfo : PermissionDenied: (\\192.168.1.100\Shared\test.txt:String) [Copy-Item], UnauthorizedAccessException> + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand 

So this did it for me:

 netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes 

Then from my host my machine in the Run window, I just did this:

 \\{IP address of nanoserver}\C$ 
0
Feb 20 '16 at 9:55
source share



All Articles