Upload local folder, its subfolders and items to site library (PowerShell)

Introduction

This Script demo shows how to upload local folder, its subfolders and items to site library.

Scenarios

Sometimes, we need to upload some file and folders. However, it is not convenient to achieve that. And I think a script will be better.‚Äč

Script

This script contains one advanced function: Import-OSCFolder. You can use this script in the following way:
Method :
  1. Rename scriptname.ps1 to scriptname.psm1 (PowerShell Module file)
  2. Run the following Import-Module cmdlet to import this module file.
            Import-Module filepath\scriptname.psm1
Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.
PowerShell
Edit|Remove
Function SubFolder($Folder,$SPFol,$spDocumentLibrary) 
{ 
    #Import the folder and subfolders to site library. 
    $SPFolder = $spDocumentLibrary.ParentWeb.GetFolder($SPFol.Folder.ServerRelativeUrl) 
    $Objects = Get-ChildItem -Path $Folder  
    Foreach($obj in $Objects) 
    { 
        If($obj.PSIsContainer) 
        {     
            $SubFolder = $spDocumentLibrary.AddItem($SPFolder.ServerRelativeUrl,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$obj.Name) 
            $SubFolder.Update() 
            $Fullname = $obj.FullName 
            SubFolder  $Fullname $SubFolder $spDocumentLibrary  
             
        } 
        Else 
        {     
            $fileStream = ([System.IO.FileInfo]$obj).OpenRead()  
            $contents = new-object byte[] $fileStream.Length  
            $FolderObj = $spDocumentLibrary.ParentWeb.GetFolder($SPFolder.ServerRelativeUrl) 
            $SpFile = $FolderObj.Files.Add($FolderObj.Url + "/"+$obj.Name, $contents, $true) 
            $spItem = $SpFile.Item 
        }}}

Examples

Example 1: Import folder "C:\Users\Administrator\Desktop\Test" to Site "http://win-lfseeatt8jr/sites/myteam".
Command: Import-OSCFolder -siteurl "http://win-lfseeatt8jr/sites/myteam" -Library "Shared Documents" -path "C:\Users\Administrator\Desktop\Test"
Screenshot:
 

Prerequisite

Windows PowerShell 2.0
SharePoint 2010