Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy/transfer file using PowerShell 2.0

Tags:

powershell

I'm trying to create a PowerShell script to copy a file from my local computer to remote computer using its IP address.

I tested my client-server connection by using this command:

Invoke Command -ComputerName <IP Address> -ScriptBlock {ipconfig} -Credential $credential

(where $credential had been entered just before this command).

I tried using the Copy-Item and Robocopy commands but I'm not clear if it will even take my credentials and then let me copy a file from a local to a remote machine. To be specific, would these even support local to remote file transfer?

A lot of times I faced errors like: Bad username and password, source path does not exist or destination path does not exist. But I still wanted to be sure if I was on right track and using the right commands to implement what I want to or if there is something else which I should consider using. How can I fix this problem?

like image 382
sanya Avatar asked Jul 08 '13 15:07

sanya


People also ask

How do I copy a file to another drive in PowerShell?

To copy items in PowerShell, one needs to use the Copy-Item cmdlet. When you use the Copy-Item, you need to provide the source file name and the destination file or folder name. In the below example, we will copy a single file from the D:\Temp to the D:\Temp1 location.

Can I use xcopy in PowerShell?

xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command.


2 Answers

It looks like you're trying to copy a file using PowerShell remoting. As posted in other answers, it would be simpler to use Copy-Item and/or Robocopy to copy from the source to a share on the destination computer.

If you want to copy the file using PowerShell remoting, you can slurp the file into a variable and use it in the remote script block. Something like:

$contents = [IO.File]::ReadAllBytes( $localPath )
Invoke-Command -ComputerName <IP Address> `
               -Credential $credential  `
               -ScriptBlock { [IO.File]::WriteAllBytes( 'C:\remotepath', $using:contents ) }

Of course, if the file you're reading is really big, this could cause the remote connection to run out of memory (by default, PowerShell limits remote connections to around 128MB).

If you're stuck using PowerShell 2, you'll need to pass the bytes as a script block parameter:

$contents = [IO.File]::ReadAllBytes( $localPath )
Invoke-Command -ComputerName <IP Address> `
               -Credential $credential  `
               -ScriptBlock { 
                   param(
                       [byte[]]
                       $Contents
                   )
                   [IO.File]::WriteAllBytes( 'C:\remotepath', $Contents) 
               } `
               -ArgumentList @( ,$contents )

And yes, you must wrap $contents in an array when passing it as the value to the -ArgumentList parameter and it must be prefixed with the comma operator ,, otherwise your remote script block will only receive the first byte.

like image 92
Aaron Jensen Avatar answered Sep 20 '22 17:09

Aaron Jensen


One of the nice things about PowerShell is (unlike DOS) it support UNC paths. So you can literary just do:

Copy-Item -Path <local file path> -Destination \\<server IP>\<share>\<path>

Of course your account will need to have access to that location. If you need to enter alternate credentials you can pre-authenticate using net use \\<server IP>\<share> /user:[<domain>\]<user> <password>

like image 39
Andy Arismendi Avatar answered Sep 18 '22 17:09

Andy Arismendi