Skip to content

Uploading files to SharePoint Revisited

Quite a long time ago I blogged about uploading files to SharePoint using PowerShell (see original article). That script used the get-content cmdlet and is very slow. The script below should work better as it uses a stream. Of course, the script is very basic and you will have to adapt it to your needs.

begin
{
$propbag=@{”ContentType=”Document”,”Product” =”Exchange 2007″ }
$docliburl=”http://sp2007/SiteDirectory/DemoSite/Shared%20Documents”
$relweburl=”/SiteDirectory/DemoSite”
[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”) > $null
$site=new-object Microsoft.SharePoint.SPSite($docliburl)
$web=$site.openweb($relweburl)
$folder=$web.getfolder($docliburl)
$list=$web.lists[$folder.ContainingDocumentLibrary]
}

process
{

$stream=[IO.File]::OpenRead($_.fullname)

$folder.files.Add($_.Name,$stream,$propbag, $true) > $null

$stream.close()
}

If you save the above script as c:\spupload.ps1 you can use the script as follows:

dir \server\share\*.* | c:\spupload.ps1

You will need to modify the script to set the variables to the correct target document library and site and to use different properties for your documents (in the $propbag).

If you don’t feel like rolling your own solution, check the following file migration products:

2 Comments

  1. Alexey wrote:

    I finally resorted to
    $my_filestream=[io.file]::ReadAllBytes($my_filename)

    Do you think OpenRead is better?

    Posted on 21-Apr-08 at 3:08 am | Permalink
  2. Alexey wrote:

    OpenRead is way more memory efficient in my recent experience. thanks for the hint.

    Posted on 04-May-08 at 7:31 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*