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();







1 comment:

  1. Hi there, possible to do this from outside a sharepoint server? From a local PC, the Add-PSSnapin Microsoft.Sharepoint.Powershell fails. From what I read, you need to be on a sharepoint server.
    https://social.technet.microsoft.com/Forums/azure/en-US/664ca8b5-551e-4956-8912-c718e1661a79/powershell-addpssnapin-produces-quotno-snapins-have-been-registered-for-windows-powershell

    My goal is to copy file from a local PC to a sharepoint URL (document library)

    Thanks

    ReplyDelete