Copy a file to all SharePoint Servers using PowerShell

Posted: October 21, 2014 in SharePoint PowerShell

Often when automating your SharePoint solutions, you will find yourself adding custom customizations, like copying a picture file to a specific location on all your SharePoint servers. To do this we can for example use the following PowerShell code.

Change the Variables under to adjust to your needs:-)

#Add SharePoint snapin:
add-PSSnapin -Name microsoft.sharepoint.powershell -EA "SilentlyContinue"


#Variables:
#List the server and file where the source file is and where it is going:
$Sourceserver = ("WFE3") 
$FromFile = "\c$\folder\file.txt"
$ToFile = "\c$\Program Files\folder\file.txt" 


#List all SharePoint Servers:
$AllSPServers = get-spserver | ? { $_.Role -eq "Application"}

#Copy specified file to each SharePoint server in the farm:
foreach ($SPServer in $AllSPServers)
    {
    $server = $SPServer.name
    $FilePathSource = "\\" + $Sourceserver + $FromFile
    $FilePathDestination = "\\" + $server + $ToFile
    Copy-Item $FilePathSource  -Destination $FilePathDestination
    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s