I am writing a powershell script that I want to run from Server A. I want to connect to Server B and copy a file to Server A as a backup.
If that can't be done then I would like to connect to Server B from Server A and copy a file to another directory in Server B.
I see they Copy-Item
command but I don't see how to give it a computer name.
I would have thought 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?
Just in case that the remote file needs your credential to get accessed, you can generate a System.Net.WebClient object using cmdlet New-Object to "Copy File Remotely", like so
Or if you need to upload a file you can do use UploadFile
This only works when PowerShell session runs under user who has rights to both administrative shares. I suggest to use regular network share on server B with readonly access to everyone and simply call (from Server A):
None of the above answers worked for me. Kept on getting this error:
So this did it for me:
Then from my host my machine in the Run box just do this \{ip of nanoserver}\C$
Why don't you use
net use
orNew-PSDrive
to create a new drive.New-PsDrive : create a new PsDrive only visible in powershell environement :
Net use : create a new drive visible in all parts of the OS.
From PowerShell version 5 onwards (included in Windows Server 2016, downloadable as part of WMF 5 for earlier versions), this is possible with remoting. The benefit of this is that it works even if, for whatever reason, you can't access shares.
For this to work, the local session where copying is initiated must have PowerShell 5 or higher installed. The remote session does not need to have PowerShell 5 installed -- it works with PowerShell versions as low as 2, and Windows Server versions as low as 2008 R2.[1]
From server A, create a session to server B:
And then, still from A:
Copying items to B is done with
-ToSession
. Note that local paths are used in both cases; you have to keep track of what server you're on.[1]: when copying from or to a remote server that only has PowerShell 2, beware of this bug in PowerShell 5.1, which at the time of writing means recursive file copying doesn't work with
-ToSession
, an apparently copying doesn't work at all with-FromSession
.