How to Upload File to SharePoint Library using PowerShell?
Requirement: Upload a File to SharePoint Library using PowerShell
We had a PowerShell script code to generate Large Lists reports in D:\Reports, and now want to periodically upload them to a document library “Reports” in my Admin site. So, scheduled a PowerShell script to do so. Here is my PowerShell code to upload the report to a document library.
How to upload file to SharePoint using PowerShell?
Do you need to upload a file to SharePoint? Did you know that you can do this using PowerShell? In this article, we will show you how to upload a file to SharePoint using PowerShell. Just specify the destination site, library name in Sharepoint where the file should be uploaded and the source file from your local path.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to Upload File
Function Upload-File($WebURL, $DocLibName, $FilePath)
{
#Get the Web & Lists to upload the file
$Web = Get-SPWeb $WebURL
$List = $Web.GetFolder($DocLibName)
#Get the Files collection
$Files = $List.Files
#Get File Name from Path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
#Delete the File from library, if already exist!
If($Files.Item($DocLibName +"/" + $FileName))
{
$Files.delete($DocLibName +"/" + $FileName)
}
#Get the File
$File= Get-ChildItem $FilePath
#Add File to the collection
$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
#Dispose the objects
$web.Dispose()
}
#call the upload function
Upload-File "https://sharepoint.company.com" "Monthly Reports" "D:\Reports\LargeLists.txt"
Upload File to SharePoint using PowerShell with Metadata:
PowerShell is a powerful scripting language that can be used to automate tasks and process data. Let me show you how to use PowerShell to upload files to Sharepoint along with its metadata. This is a great way to quickly and easily add files to your SharePoint site.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to Upload File and set metadata of the file
Function Upload-FileWithMetadata($WebURL, $DocLibName, $FilePath)
{
#Get the SharePoint Web and Lists to upload the file
$Web = Get-SPWeb $WebURL
$List = $Web.GetFolder($DocLibName)
#Get the Files collection from SharePoint Document Library
$Files = $List.Files
#Get File Name from Path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)
#Get the File from Disk
$File= Get-ChildItem $FilePath
#Set the Metadata
$Metadata = @{}
$Metadata.add("Department", "Sales")
$Metadata.add("Location", "APAC");
#powershell to upload file to sharepoint with metadata
$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(), $Metadata, $true) #true for overwrite file, if already exists!
<# Alternatively, You can set the metadata as:
$UploadedFile=$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(), $true)
$UploadedFile.Item["Department"]="Sales"
$UploadedFile.Item.Update()
#>
}
#call the upload function
Upload-FileWithMetadata "https://sharepoint.company.com" "Monthly Reports" "E:\Reports\UsageReport.csv"
If you are looking for a way to upload files to SharePoint List or Library using C#, have a look at my other post: How to Programmatically upload File to SharePoint document library?
If you want to upload Multiple Files in Bulk, Refer: Bulk upload files to SharePoint using PowerShell
Do you have a SharePoint Online (O365) version for these scripts (especially the ‘with metadata’ version’)?
Sure, Here is the script: PowerShell to Upload a File to SharePoint Online
Hello,
Is it possible to run the above script where SharePoint is not installed?
Actually i am trying to upload documents to sharepoint site by running a script in non sharepoint server but its failing with an error.
Could you please help me in achieving this.
Thanks in Advance.
Hi There,
If you want to upload files to SharePoint remotely, Here are the options:
Upload Files to SharePoint Library Remotely using Client Object Model (CSOM) and PowerShell
Upload Files to SharePoint Remotely using Web Client and PowerShell>
Upload File to SharePoint using Web Services and PowerShell
can it taake metadata from excel ?
Sure, Use this as an example: Migrate SharePoint 2010 Document Libraries to SharePoint Online using PowerShell
Will this work if I want to copy a bunch of files to a sharepoint folder inside a library ?
Also getting this error
Exception getting “Item”: “urlOfFile
Parameter name: Specified value is not supported for the urlOfFile parameter.”
At line:33 char:4
+ if($Files.Item($DocLibName +”/” + $FileName))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], GetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenGetting
Hi Sal, is it possible to use this to update an item that is in a Managed Metadata column? I am able to get do a text entry just fine thanks to your script, but it will not work on Managed Metadata in a Term Store.
True Ryan! Managed Metadata columns can’t be used like normal list columns.
To set Managed Metadata Column with PowerShell, Refer this article: Updating SharePoint Managed Metadata Columns with PowerShell
That’s some prompt answering, Sal! Tried it al, works a charm. Thanks, your help is greatly appreciated! Regards, Jan
Oops, I forgot another question: is it possible to set the Content Type used when uploading?
Yes, You can set the Content Type for your items while uploading. Here is how:
#Get the Content Type
$ctype = $web.ContentTypes[“YOUR-Content-Type”];
#Set Content Type Property
$Metadata.add(“ContentTypeId”, $ctype.Id);
Regards,
Sal
Hi Salaudeen, i tried to update the “Title” column of a document item with both your methods, but without any success, unfortunately. Am I doing something wrong?
First method: $Metadata.add(“Title”, “SomeValue”) – Result: Title stays empty. Other metadata works fine!
Second method: $UploadedFile.Item[“Title”]=”SomeValue” – Result: error message about Null valued array.
Thanks for a great and helpful article, hope you can help a little bit more. Thanks in advance, and regards from the Netherlands!
Hi Jan,
Just verified the second method, Works as expected:
$UploadedFile=$Files.Add($DocLibName +”/” + $FileName,$File.OpenRead(), $true)
$UploadedFile.Item[“Title”]= “Title Value Goes here”
$UploadedFile.Item.Update()
Nice article Salaudeen, it really helped me a lot! However, I’m experiencing some problems when trying to update the “Title” column of a document. I just added the line:
$Metadata.add(“Title”, “SomeValue”)
but nothing happened. Tried the second method as well:
$UploadedFile.Item[“Title”]=”SomeValue”
but again no success. Any idea what I might be doing wrong? Thanks in advance!
Regards, Jan
Hi Jan,
For Title Field, Use: “vti_title” when you use Hash-table.
$Metadata.add(“vti_title”, “Your Title Value”)
What if the file has a metadata associated with it ? For e.g. if it has a “Month” field that need to be filled up while uploading the document ?
Hi KR,
Updated the post with setting Metadata values in PowerShell.
Hope it helps.
good