Software Engineer

My photo
Colombo, Sri Lanka
B.Sc.Special(Hons)in IT, MCTS, MCPD, MCP

Thursday, May 23, 2013

Add a file to the root of a SharePoint site using PowerShell

Sometimes you need a file to be right off the root of your Internet facing SharePoint site. Files like robots.txt, sitemap.xml. To do this we’ll take advantage of PowerShell’s ability to use any .NET methods along with the Files collection on each SPWeb in SharePoint.

Copy the following syntax and change the values marked in red color according to your need. In the first one give the full path to your file. In the second place with this file name you are going to save your file in the root.

$fileBytes = [system.io.file]::ReadAllBytes("c:\the\full\path\to\your\file.txt");
$site = Get-SPSite "http://sharepoint1";
$site.RootWeb.Files.Add("robots.txt", $fileBytes, $true);


Open the PowerShell as administrator then just copy and execute.
This will result in a robots.txt located at “http://yourdomain:portifneeded/file.txt”.

Be Happy !!! :)

No comments:

Post a Comment