Uploading Files to a libary in Sharepoint 3.0 with Powershell

Posted on 2010-01-12
Medium Priority
Last Modified: 2012-05-08
Hi, I have trawled the net looking for ways to automate uploads of a doc and docx files from a folder and have them uploaded to the sharepoint document libary, here is a script we used to use a while back, but doesnt seem to work anymore as i recieve this error

D:\Upload>powershell gci D:\Upload\*.doc | upload.ps1
New-Object : Exception calling ".ctor" with "1" argument(s): "The Web applicati
on at http://iccintranet/it/Knowledge%20Base could not be found. Verify that yo
u have typed the URL correctly. If the URL should be serving existing content,
the system administrator may need to add a new request URL mapping to the inten
ded application."
At D:\Upload\upload.ps1:8 char:18
+  $site=new-object  <<<< Microsoft.SharePoint.SPSite($docliburl)
You cannot call a method on a null-valued expression.
At D:\Upload\upload.ps1:9 char:20
+  $web=$site.openweb( <<<< $relweburl)
You cannot call a method on a null-valued expression.
At D:\Upload\upload.ps1:10 char:24
+  $folder=$web.getfolder( <<<< $docliburl)
Cannot index into a null array.
At D:\Upload\upload.ps1:11 char:19
+  $list=$web.lists[$ <<<< folder.ContainingDocumentLibrary]
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At D:\Upload\upload.ps1:18 char:20
+  $bytes=get-content  <<<< $_ -encoding byte
You cannot call a method on a null-valued expression.
At D:\Upload\upload.ps1:20 char:19
+  $folder.files.Add( <<<< $_.Name,$bytes,$propbag, $true) > $null

Here is the code

If anyone has another example I could try,
By the way it was working on 32bit Windows and we are currently running 64bit server 2008


                       MyCol =PowerShell About Docs }
 [System.Reflection.Assembly]::LoadWithPartialName(Microsoft.SharePoint) > $null
 $site=new-object Microsoft.SharePoint.SPSite($docliburl)


 ## I expect FileInfo objects in the pipeline
 $bytes=get-content $_ -encoding byte
 $folder.files.Add($_.Name,$bytes,$propbag, $true) > $null

Open in new window

Question by:Illawarra
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 16

Expert Comment

ID: 26299025
How about keeping it really simple and just use the copy command?

Something like:

copy *.doc \\server\sites\sitename\doclibname

The Web Client service needs to be running to be able to access the document lib via UNC, which is the default on workstation OSes like XP but is disabled on Server OSes.

Author Comment

ID: 26299249
Yeah, that works
but what about permissions when they need to be applied etc..?
LVL 16

Expert Comment

ID: 26299324
Not sure I understand your question about permissions...
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 26299397
It's just bascially about uploading with a user name and password, and carrying over the all important metadata because "aparantly" the metadata screws up if its just coppied into the UNC path?
LVL 16

Expert Comment

ID: 26299449
Regarding "permissions", whatever is running the process will be the account used to upload the document. You can probably use a runas command if you need to specify a different account.

Metadata wasn't mentioned in your original question and wasn't part of the code snippet. :) Was your powershell script doing that?


Author Comment

ID: 26299483
ah, I actually have no idea as it was all working a couple of years ago but since moving servers my manager has told me to research a way to import metadata along with the documents and I found this old script laying around,

you think you could point me in the direction of something that could be of use please? :)
LVL 16

Expert Comment

ID: 26299507
When you say "was all working", are you saying the script set the metadata also? How would the script know what field/value to set? Would love to see the script.

I think you should post your "upload document and set metadata" as a separate question, so that you can get more participation
LVL 26

Accepted Solution

rdcpro earned 2000 total points
ID: 26301170
Usually when you're migrating documents, the metadata is all over the place.  I don't see how a powershell script would work, unless you created something for it to read the metadata from.  
If you're going to do that, why re-invent the wheel? I've used this tool in the past:
It can be used in a variety of ways, but I imported thousands of files for a client by creating a set of spreadsheets with the metadata for the files, and one column with the path to the file.  A simple DOS command got me a list of all the files in a form that worked, and I just pasted it in the spreadsheet.  
dir *.* /b /s > files.txt
Then based on where the document was stored, I set up metadata for each document.  Excel made it pretty easy to do, especially where one piece of metadata was repeated over a lot of lines.  
Once I had that ready DocKIT ripped through the spreadsheet line-by-line, uploaded each file, updated the metadata, and then checked the file in.  
IMO, it's well worth the money, but you can always give the trial version a try.  We just had a vendor create something similar for Documentum, and it cost a bundle.
Mike Sharp

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For SharePoint sites, particularly public-facing ones, there are times when adding JavaScript, Meta Tags, CSS Styles or other content to the page <head> section is more practical than modifying master pages.  For instance, you could add the jQuery l…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question