Im trying to upload a file to a document library in SharePoint 2010 via powershell. Ive followed about 5 different blog posts, most recently the one found here - http://www.sharepoint-howto.com/2012/03/14/sharepoint-2010-how-to-add-files-in-a-folder-to-a-sharepoint-library-with-powershell/
This is my powershell script, but the file just wont upload.
$siteurl = "http://intranet.org/IT"
$spWeb = Get-SPWeb $siteurl
$docLibraryName = "Shared Documents"
$localFolderPath = "C:\ServerStatus\Output"
$docLibrary = $spWeb.Lists[$docLibraryName]
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object {
$fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
$contents = new-object byte[] $fileStream.Length
$fileStream.Read($contents, 0, [int]$fileStream.Length);
$fileStream.Close();
write-host "Copying" $_.Name "to" $docLibrary.Title "in" $spWeb.Title "..."
$folder = $docLibrary.RootFolder
$spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
$spItem = $spFile.Item
}
When I run the commands individually, the variables $spFile and $spItem are empty, any idea's why these commands would fail?