Today I got a bit creative and threw together a small example for my students about how you can use powershell to upload files to Sharepoint. The script doesn’t have any error handling or anything of the sort, I leave that up to whomever uses the code as a bas.. . It takes a site url, and library name as parameters and then uploads the argument list. The file is also attached to this post.
-
param($site=$(throw “You must specify a site”), $list=$(throw “You must specify a list”))
-
if ($args.count -le 0) { throw “You must supply atlest one file to upload” }
-
-
[void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
-
$spSite = New-Object Microsoft.SharePoint.SPSite($site)
-
$spWeb = $spSite.OpenWeb()
-
$spList = $spWeb.GetFolder($list)
-
$spFiles = $spList.Files
-
-
for ($i=0; $i -le $args.count;$i++)
-
{
-
$fileInfo = Get-ChildItem $args[$i]
-
$fileContent = Get-Content $fileInfo -encoding byte
-
$fileName = “{0}/{1}” -f $list, $fileInfo.Name
-
$fileMetadata = New-Object HashTable
-
$spFile = $spFiles.Add($fileName, $fileContent, $fileMetadata, $true)
-
}
Inspired by Sharepoint MVP Tobias Zimmergren and his example of doing it in C#