Copy-Item has no -ComputerName parameter. Open a session and pass it:
Copy-Item -Path C:\local\file.txt -Destination C:\remote\path -ToSession (New-PSSession -ComputerName server01).
As a MS SQL DBA, it’s often necessary to copy files to remote servers. Backups, a script you need on a box that has no source control, an installer for something the remote host is missing. The command that gets suggested for this is usually wrong, so start with that.
The Command That Does Not Work
# this fails Copy-Item -Path $localFile -ComputerName $remoteComputer -Destination 'C:\remote\path'
Copy-Item : A parameter cannot be found that matches parameter name 'ComputerName'.
There is no -ComputerName on Copy-Item, in Windows PowerShell 5.1 or
in PowerShell 7. Plenty of cmdlets have one, which is why the guess is so common, but the file
system provider does remoting through a session object instead. You can settle it yourself on any
machine:
(Get-Command Copy-Item).Parameters.Keys -match 'Session|ComputerName'
You get back FromSession and ToSession, and nothing else.
Copy Files to a Remote Computer
$session = New-PSSession -ComputerName 'server01' -Credential 'DOMAIN\username' # one file Copy-Item -Path 'C:\local\path\file.txt' -Destination 'C:\remote\path\' -ToSession $session # a whole folder Copy-Item -Path 'C:\local\path\scripts' -Destination 'C:\remote\path\' -ToSession $session -Recurse Remove-PSSession $session
2 things about that -Destination catch people out. It is a path on the remote
machine, not a UNC share, so you write C:\remote\path and mean the remote C drive.
And the direction is set by the parameter, not the paths: swap -ToSession for
-FromSession and the same command pulls the file back instead, with
-Path now meaning the remote side.
New-PSSession you never remove sits open on both ends. In a script that loops over
servers, that is 1 live session per host until the shell exits, and on the remote side those
count against the WinRM shell limit for your account.Copy Files to Multiple Hosts
New-PSSession takes a list of computers and gives you a session for each one, but
-ToSession takes exactly 1 session. So the loop is around the copy, not around the
connect:
$servers = 'server01', 'server02', 'server03'
$sessions = New-PSSession -ComputerName $servers -Credential 'DOMAIN\username'
foreach ($session in $sessions) {
Copy-Item -Path 'C:\local\path\file.txt' `
-Destination 'C:\remote\path\' `
-ToSession $session
}
Remove-PSSession $sessionsIf a host is unreachable, New-PSSession reports that host and carries on with the
rest, so $sessions can be shorter than $servers. Compare the 2 counts
before you report the job as done, otherwise a server that was down at the time quietly gets
skipped:
$missing = $servers | Where-Object { $_ -notin $sessions.ComputerName }
if ($missing) { "no session: $($missing -join ', ')" }When Not to Use It
Copying over a session sends the file through the WinRM channel, which is fine for scripts and
installers and poor for large files. For a database backup, an administrative share and
robocopy will be faster and restartable, and neither of those is true of
Copy-Item:
robocopy "D:\Backup" "\\server01\D$\Backup" full.bak /R:2 /W:5
The session route earns its place when the shares are locked down and WinRM is the only way in, which on a hardened SQL Server estate is often exactly the situation.
Microsoft’s Copy-Item reference carries the full parameter set and worked examples for both directions.
Related
If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.
Leave a Reply