Showing posts with label Upload files to document library using Powershell. Show all posts
Showing posts with label Upload files to document library using Powershell. Show all posts

Thursday, 24 July 2014

Add a file to a document library using PowerShell / Upload Files to Document Library Using Powershell


Add files in a Folder to a Document Library using PowerShell

Below Powershell Script will upload the files in your Local Drive to SharePoint Document library.

Here i am Uploading to Library named Documents from D drive , 
cls

asnp "*sh*"

$url=Read-Host "Enter Site Url"

$web=Get-SPWeb -Identity $url

if($web)
{
    try
    {
    $list = $web.Lists.TryGetList("Documents")

    $files = Get-ChildItem -Path "D:\Manju" -Force -Recurse

        foreach ($file in $files)
        {
          $stream = $file.OpenRead()

          $done= $list.RootFolder.Files.Add($file.Name, $stream, $true)
          
          Write-Host $done.Name  "Uploaded into the Site" -BackgroundColor Green         

        }
    }
    catch
    {
    $ErrorMessage = $_.Exception.Message
    Write-Host $ErrorMessage -BackgroundColor Yellow 
    }
  }

  else
  {
  Write-Host "Site Doesn't exist"
  }

$list.Update();