{"id":3741,"date":"2013-04-28T00:01:00","date_gmt":"2013-04-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/04\/28\/weekend-scripter-use-powershell-to-upload-a-new-file-version-to-sharepoint\/"},"modified":"2013-04-28T00:01:00","modified_gmt":"2013-04-28T00:01:00","slug":"weekend-scripter-use-powershell-to-upload-a-new-file-version-to-sharepoint","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-upload-a-new-file-version-to-sharepoint\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Upload a New File Version to SharePoint"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft PowerShell MVP, Niklas Goude, talks about using Windows PowerShell to upload a new version of a file to SharePoint.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today Niklas Goude is our guest blogger. You can read more from Niklas in his <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/niklas+goude\/\" target=\"_blank\">past Hey, Scripting Guy! Blog posts<\/a>.<\/p>\n<p>Take it away Niklas&hellip;<\/p>\n<p>In a previous post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/04\/30\/3354785.aspx\" target=\"_blank\">Use PowerShell Cmdlets to Manage SharePoint Document Libraries<\/a>, we talked about uploading files to a share in SharePoint 2010.&nbsp;<span style=\"font-size: 12px\">Now we&#8217;ll take this a step further and update a minor or a major version of an existing document.<\/span><\/p>\n<p>Here&rsquo;s a quick recap of the code used to upload the file:<\/p>\n<p style=\"padding-left: 30px\"># Add the Snapin<br \/> Add-PSSnapin Microsoft.SharePoint.PowerShell<\/p>\n<p style=\"padding-left: 30px\"># Retrieve specific Site<br \/> $spWeb = Get-SPWeb http:\/\/SP01<\/p>\n<p style=\"padding-left: 30px\"># Create instance of Folder<br \/> $spFolder = $spWeb.GetFolder(&#8220;Shared Documents&#8221;)<\/p>\n<p style=\"padding-left: 30px\"># Get the file on Disk that we want to upload<br \/> $file = Get-Item C:\\Documents\\MyDoc.docx<\/p>\n<p style=\"padding-left: 30px\"># upload the file.<br \/> $spFolder.Files.Add(&#8220;Shared Documents\/MyDoc.docx&#8221;,$file.OpenRead(),$false)<\/p>\n<p>What we&rsquo;ve done so far is to upload a single document to a document library in SharePoint 2010. The file used in this example is stored on drive C:&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7041.wes-4-28-13-1.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7041.wes-4-28-13-1.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>&nbsp;&nbsp;<\/p>\n<p>The document contains a single line:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5165.wes-4-28-13-2.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5165.wes-4-28-13-2.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>After we run the Windows PowerShell code, the document gets uploaded to a document library in SharePoint 2010.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8551.wes-4-28-13-3.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8551.wes-4-28-13-3.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>We can check the versioning for the document by clicking Version History in SharePoint:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0066.wes-4-28-13-4.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0066.wes-4-28-13-4.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>We&rsquo;ll see that the version number is set to 0.1:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8802.wes-4-28-13-5.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8802.wes-4-28-13-5.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Now, let&rsquo;s open the file that is stored on drive C and modify it:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0602.wes-4-28-13-6.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0602.wes-4-28-13-6.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>Back in Windows PowerShell, we use <strong>SPFileCollection<\/strong> to pick up the document we just uploaded. In this example, we are going to filter out the document where the name is equal to MyDoc.docx by using the <strong>Where-Object<\/strong> cmdlet.<\/p>\n<p style=\"padding-left: 30px\"># Retrieve specific Site<br \/> $spWeb = Get-SPWeb http:\/\/SP01<\/p>\n<p style=\"padding-left: 30px\"># Create instance of Folder<br \/> $spFolder = $spWeb.GetFolder(&#8220;Shared Documents&#8221;)<\/p>\n<p style=\"padding-left: 30px\"># Retrieve a Specific File.<br \/> $spFile = $spFolder.Files | Where-Object { $_.Name -eq &#8220;MyDoc.docx&#8221; }<\/p>\n<p><span style=\"font-size: small\">We pick up the file because we want to use some of its properties for the new version that we want to upload. When we upload a new version of the document, we still use the <strong style=\"font-size: 12px\">Add<\/strong> method that is provided by the Microsoft.SharePoint.SPFileCollection, but with a different overload definition. Here&rsquo;s the definition on MSDN: <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms448353.aspx\" target=\"_blank\">SPFileCollection.Add method (String, Stream, SPUser, SPUser, DateTime, DateTime)<\/a><span style=\"font-size: 12px\">.<\/span><\/p>\n<p>The parameters we want to use are: <strong>urlOfFile<\/strong>, <strong>File<\/strong>, <strong>CreatedBy<\/strong>, <strong>ModifiedBy<\/strong>, <strong>TimeCreated<\/strong>, and <strong>TimeLastModified<\/strong>.<\/p>\n<p>We can get the following values from the existing document in SharePoint 2010: <strong>UrlOfFile<\/strong>, <strong>CreatedBy<\/strong>, <strong>ModifiedBy<\/strong>, and <strong>TimeCreated<\/strong>. We can get <strong>File<\/strong> by using <strong>Get-Item<\/strong>, and we can get <strong>TimeLastModified<\/strong> by simply using <strong>Get-Date<\/strong>.<\/p>\n<p>First, let&rsquo;s get the modified MyDoc.docx from drive C:<\/p>\n<p style=\"padding-left: 30px\">$file = Get-Item C:\\Documents\\MyDoc.docx<\/p>\n<p>Next we run the method:<\/p>\n<p style=\"padding-left: 30px\">$newVersion = $spFolder.Files.Add($spFile.Name, $file.OpenRead(), $spFile.Author, $spFile.ModifiedBy, $spFile.TimeCreated, (Get-Date))<\/p>\n<p>If we open the SharePoint document library again, we&rsquo;ll see that we have a new minor version of our document, .02.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6710.wes-4-28-13-7.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6710.wes-4-28-13-7.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>And finally, if we want to add the modified document as a major version, we use the <strong>Publish<\/strong> method:<\/p>\n<p style=\"padding-left: 30px\">$newVersion.Publish(&#8220;&#8221;)<\/p>\n<p>Now we have a new major version, 1.0, instead.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8357.wes-4-28-13-8.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8357.wes-4-28-13-8.jpg\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>~Niklas<\/p>\n<p>Thank you, Niklas, for taking your time to share your knowledge.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft PowerShell MVP, Niklas Goude, talks about using Windows PowerShell to upload a new version of a file to SharePoint. Microsoft Scripting Guy, Ed Wilson, is here. Today Niklas Goude is our guest blogger. You can read more from Niklas in his past Hey, Scripting Guy! Blog posts. Take it away Niklas&hellip; In a [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,183,3,130,59,61,45],"class_list":["post-3741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-niklas-goude","tag-scripting-guy","tag-servers","tag-sharepoint","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft PowerShell MVP, Niklas Goude, talks about using Windows PowerShell to upload a new version of a file to SharePoint. Microsoft Scripting Guy, Ed Wilson, is here. Today Niklas Goude is our guest blogger. You can read more from Niklas in his past Hey, Scripting Guy! Blog posts. Take it away Niklas&hellip; In a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3741"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3741\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}