Daily SharePoint site testing, we often have to do a variety of data, and today wrote a few scripts, found themselves writing more and more scripts, so I decided to tidy up, and some of the commonly used reusable methods are published.
Today, let's talk about how to upload files to a SharePoint library using PowerShell, with the following code:
add- pssnapin Microsoft.SharePoint.PowerShellfunction createagendadocumentdata{param ($siteUrl, $listTitle,$filePath,$fileName) $site= get-SPSite $siteUrl $web=$site. RootWeb $List=$web. lists[$listTitle] $folder=$List. RootFolder $File= get-ChildItem $filePath $fileStream= ([System.IO.FileInfo] (get-Item $File. FullName)). OpenRead ()[Microsoft.SharePoint.SPFile] $spFile= $folder. Files.add ($folder. URL +"/"+$fileName, [System.IO.Stream] $fileStream, $true) $spFile. Item.update () $fileStream. Close ()}
The orange font in the above code is the information you need to enter, including the url,list title of SharePoint site, the path to the local file, and the new name you want to upload to SharePoint.
The following methods are used:
" fill in the site URL for the SharePoint sites " "fill in the title of the list of files you want to uploadand fill in the local file path you want to upload " -siteurl $siteUrl-listtitle $listTitle-filepath $filePath
You can then see that the file has been uploaded to the specified list (library).
In fact, in the actual operation, sometimes not only to upload files, but also to its location item set relevant field value, this I will be a separate article to explain all the relevant operations.
If you find it helpful, please, I will write a series of articles about implementing SharePoint Automation with PowerShell. You are welcome to communicate with me and ask me questions.
SharePoint site test data Automation series--upload files to SharePoint library using PowerShell.