Thursday 12 September 2013

Power Shell Script to copy a File to all sub webs to the Folder location:


 cls

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
get-PSSnapin Microsoft.SharePoint.PowerShell

Start-SPAssignment -Global
#-------------------------------------------

#static parameters
$site = Get-SPSite "http://sp2013:90/sites/test/"

$Web = Get-SPWeb "http://sp2013:90/sites/test/"

$Sourcefile = $Web.GetFile("http://sp2013:90/sites/test/DocSlide/youfilename.txt");

$TargetFolderName = "Site Assets"

#end of Static parameters

foreach($subsite in $Web.Webs)
{
Write-output("Web Title: "+ $subsite.Title)

$list = $subsite.Lists.TryGetList($TargetFolderName)

if($list -ne $null)
{
if($list.Folders.Count >0)
{
foreach($folder in $list.Folders)
{
if($folder.Name -eq $TargetFolderName)
{
CopyToFolder( $folder.Url,$subsite)
}
}
}
else
{
write-output("Folder is not available ")
}
}
}
function CopyToFolder()
{
param($folderUrl)
param($subsite)

$DesFolder = $subsite.GetFolder($TargetFolderName);

$content = $Sourcefile.OpenBinary()

$Destiantion = $subsite.GetFolder($folderUrl)

write-output(" Destination Folder Name: "+$folder.url)

$newFile = $Destiantion.Files.Add($Sourcefile.Name, $content ,$false)

$DesFolder.Update();
}
$site.Dispose();
Stop-SPAssignment-Global 

No comments:

Post a Comment