# encoding: ascii
# api: powershell
# title: Publish-File
# description: Use this to upload one or more files to a SharePoint document library. Should also work with any WebDAV service, although that hasn’t been tested. The filename parameter expects fileinfo objects. Easiest way to do so is to pass them on the pipeline from Get-ChildItem. This script is a refinement of a technique that I first saw here: http://blogs.flexnetconsult.co.uk/colinbyrne/PermaLink,guid,a326572f-8f78-4c80-86d5-1fe52cbd6fe5.aspx.
# version: 0.1
# type: function
# author: halr9000
# license: CC0
# function: Publish-File
# x-poshcode-id: 2122
# x-archived: 2016-06-29T20:02:17
# x-published: 2011-09-07T09:54:00
#
# The credential parameter is optional. If you do not supply it, then your currently logged-in credentials are used.
#
# Note that this version will not descend directories.
function Publish-File {
param (
[parameter( Mandatory = $true, HelpMessage="URL pointing to a SharePoint document library (omit the '/forms/default.aspx' portion)." )]
[System.Uri]$Url,
[parameter( Mandatory = $true, ValueFromPipeline = $true, HelpMessage="One or more files to publish. Use 'dir' to produce correct object type." )]
[System.IO.FileInfo[]]$FileName,
[system.Management.Automation.PSCredential]$Credential
)
$wc = new-object System.Net.WebClient
if ( $Credential ) { $wc.Credentials = $Credential }
else { $wc.UseDefaultCredentials = $true }
$FileName | ForEach-Object {
$DestUrl = "{0}{1}{2}" -f $Url.ToString().TrimEnd("/"), "/", $_.Name
Write-Verbose "$( get-date -f s ): Uploading file: $_"
$wc.UploadFile( $DestUrl , "PUT", $_.FullName )
Write-Verbose "$( get-date -f s ): Upload completed"
}
}
# Example:
# dir c:\path\files* | Publish-File -Url "https://mysharepointsite.com/personal/userID/Personal%20Documents"