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
I finally resorted to
$my_filestream=[io.file]::ReadAllBytes($my_filename)
Do you think OpenRead is better?
OpenRead is way more memory efficient in my recent experience. thanks for the hint.
Post a Comment