Copy a file using PowerShell

In this tutorial, I show you how you can copy a file using PowerShell from one location to another location. I also show you how to copy a file to multiple servers using PowerShell.

Copy a file using PowerShell

Copy a file using PowerShell from one location to another

The copy-item cmdlet is used to copy an item or file from one location to another location. The copy-item cmdlet has several parameters which can be pass along with the source and destination path.

Let’s use copy-item cmdlet to copy a file using PowerShell

$SourcefilePath = “C:\sourcepath\test1.txt”
$folderPathDest = “C:\destpath\”
Copy-Item -Path $SourcefilePath -Destination $folderPathDest –PassThru

This is very basic the file in the source path is copied to the destination path using the above command. Now the requirement is you have to copy a file using PowerShell to multiple servers. You have a list of servers and the file should copy to a particular folder in all the listed servers.

Copy file to multiple servers using PowerShell

To make this script generic and can be used later so what I did I have created one CSV file that has all the server names like below and we use this file as an input to our script –

Below is the server name for demo purposes (these are dummy names you can change the names as per your environment)

ServerName
CABF11V0500
CABF11V0501
CABF11V0502
CABF11V0503
CABF11V0504

Refer below screen shot –

Copy file to multiple servers using PowerShell

Once you have listed all the servers where you want to copy the file then save this input CSV file as servernames.csv.

Note – You must log in to the PowerShell management shell to the account who have access to all these five servers.

Below is the script that copies a file to all the servers mentioned in the input CSV file in one go.

#Get root path of ps script
$logfilePath = split-path -parent $MyInvocation.MyCommand.Definition
#Source file which need to be copied over the servers
$SourcefilePath = $logfilePath + "\sourcefile.txt"
#Create log file if any exception while copying
$logfilePath=$logfilePath+"\CopyLogs.txt"
#Clear content of file.
if(Test-Path $logfilePath)
{
 Clear-Content $logfilePath
}
$pathfld = split-path -parent $MyInvocation.MyCommand.Definition;
$fullpath = Join-Path $pathfld "\servernames.csv"
Import-Csv -Delimiter "," -Path $fullpath | % {
    $folderPathDest="\\"+$_.ServerName +"\c$"+ "\destlocation\"
    try
    {

            Copy-Item -Path $SourcefilePath -Destination $folderPathDest -PassThru
            Write-Host "File Copied Successfully!" -ForegroundColor Green
    }
    catch
    {
        write-host $Field "Error Occurred  ....... " $($_.Exception.Message) -foregroundcolor Red
        "Error Occurred : $($_.Exception.Message)" | Out-File $logfilePath -Append
        }
    }

Once executed successfully the script will copy the “sourcefile.txt” in all the servers listed in an input CSV file. If any exception occurs you can refer to the copyLogs.txt file.

That’s it the file will be copied to destination “c drive location” mentioned in the above script you can verify the file on all the servers.

Conclusion

This is how you can copy a file using PowerShell to one machine or multiple machines. This is helpful when you have multiple servers in a single farm and need to copy something on all the locations using the PowerShell script.

Leave a Reply

Your email address will not be published.