Thursday, 6 March 2014

PowerShell Script To Upload Data From Local Drive To SharePoint Document Library

Below Script uploads all The Document from Your Local Drive Folder To SharePoint Document Library with error log file


if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Script settings

$webUrl = "http://xyz:4444/"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared Documents\Test"

$localFolderPath = "C:\\Test\\Test"

#Open web and library

$web = Get-SPWeb $webUrl

$docLibrary = $web.Lists[$docLibraryName]

$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()

ForEach($file in $files)
{

    #Open file
    try
    {
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

    #Add file
    $folder =  $web.getfolder($docLibraryUrlName)

    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"

    #Close file stream
    $fileStream.Close();
    }
    catch
    {
    Write "Error: $file.name: $_" >>c:\logfile.txt
        continue;
    }
}

#Dispose web

$web.Dispose()

 

No comments:

Post a Comment