Online Training On SharePoint
                      

Wednesday, 2 June 2010

Uploading a Document using Power Shell Script

To upload a single document to a document library use the following PowerShell script. This is very helpful if we want to automate deployment of files during deployment of code.
The function accepts three parameters:
1. url points to url of the sharepoint site.
2. folder points to Document Library Name
3. Document points to the Path of the file.

function Upload-SPDocument([string]$url, [string]$Folder, [string]$Document) 
{ 
        $SPSite = New-Object Microsoft.SharePoint.SPSite($url) 
        $OpenWeb = $SPSite.OpenWeb() 
        $DocumentName = Split-Path $Document -Leaf 
        $GetFolder = $OpenWeb.GetFolder($Folder) 
        [void]$GetFolder.Files.Add("$Folder/$DocumentName",$((gci $Document).OpenRead()),"") 
        $OpenWeb.Dispose() 
}
Post a Comment
Related Posts with Thumbnails