Uploading File to Sharepoint 2010 using PS

Ok, I'm a novice to both Sharepoint and Powershell.  Have looked up many ways to script uploading a file to Sharepoint.  So here's where I am now stuck.  Here is the script:


if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}
 
#Script settings
 
#SiteName
$webUrl = "http://sharepointserver
 
#SharePoint Library Name
$SPLibraryName = "Documents"
 
#library URL  
$SPLibraryUrl = "http://sharepointserver/sites/ost/documents
 
#Location where from you want to upload file to SharePoint lib.
$localFolderPath = "C:\Dockets"
 
 
 
#Open web and library
 #Site URL
$web = Get-SPWeb $webUrl
 #Libray Name
$docLibrary = $web.Lists[$SPLibraryName]
 
 #Getting All Files
 $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
 
ForEach($file in $files)
{
 
    #Open file
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
 
    #Add file
    $folder =  $web.getfolder($SPLibraryUrl)
 
    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"
 
    #Close file stream
    $fileStream.Close();
}
 
#Dispose web
 
$web.Dispose()

Here is the error I get:  

Exception calling "Add" with "3" argument(s): "<nativehr>0x80070003</nativehr><nativestack></nativestack>There is no file with URL 'http://sharepointserver/sites/ost/documents/<filename>,pdf' in this
Web."
At C:\Scripts\CopyFilesSP.ps1:41 char:32
+     $spFile = $folder.Files.Add <<<< ($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
garberj76Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

garberj76Author Commented:
I should clarify this is Sharepoint 2010 and I'm running this on the server where Sharepoint is hosted.  I apology in advance, I am very new to Sharepoint.
Bob LearnedCommented:
I read here, and it looks like you just need the file name, and not the folder URL.

Weekend Scripter: Use PowerShell to Upload a New File Version to SharePoint
http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/28/weekend-scripter-use-powershell-to-upload-a-sharepoint-file-version.aspx

# Add the Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell

# Retrieve specific Site
$spWeb = Get-SPWeb http://SP01

# Create instance of Folder
$spFolder = $spWeb.GetFolder("Shared Documents")

# Get the file on Disk that we want to upload
$file = Get-Item C:\Documents\MyDoc.docx

# upload the file.
$spFolder.Files.Add("Shared Documents/MyDoc.docx",$file.OpenRead(),$false)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garberj76Author Commented:
Thanks, Bob.  Sorry for the delay.  For now, this seems to have fixed my problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.