Skip to content

Instantly share code, notes, and snippets.

@matt40k
Created July 7, 2016 14:28
Show Gist options
  • Save matt40k/52e9b150dc69c2283a311238f0f888fd to your computer and use it in GitHub Desktop.
Save matt40k/52e9b150dc69c2283a311238f0f888fd to your computer and use it in GitHub Desktop.
# 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment