Monthly Archives: May 2013

How to import (upload) an entire folder of files to SharePoint using PowerShell

SharePoint PowerShell How To Series – Foreword

I often find myself scratching around the local drive of my development machines for remnants of PowerShell scripts to generate list & libraries in SharePoint with all sorts of different folder hierarchies to test the performance of code I’m working on to ensure it scales well once the folder & item counts start getting up to high numbers. So rather than keep scratching I’m going to start posting my scripts as I create and use them so I can find/reuse them later on and someone else might find them useful as well. I’m sure they won’t work under all scenarios and situations (and they are not designed to). They are just quick scripts that serve a specific purpose to me at the time and you may be able to tweak to fit your needs.

Note this example doesn’t handle folders within folders. There are also several “no-code” methods for uploading a whole folder structure (and files) to SharePoint.

1. Create a folder containing the files to upload to SharePoint

files-to-import-upload-to-sharepoint-cameron-dwyer

2. Locate the SharePoint library that you want to upload the files to, take note of the:

  • Web URL
  • Document library URL
  • Document library name

3. Copy the following script to be run in a PowerShell window

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Script settings

$webUrl = "http://vs-server12"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared%20Documents"

$localFolderPath = "C:\temp\Docs4SharePoint"

#Open web and library

$web = Get-SPWeb $webUrl

$docLibrary = $web.Lists[$docLibraryName]

$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()

ForEach($file in $files)
{

    #Open file
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

    #Add file
    $folder =  $web.getfolder($docLibraryUrlName)

    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"

    #Close file stream
    $fileStream.Close();
}

#Dispose web

$web.Dispose()

4. Change the PowerShell variables at the top of the script to match your SharePoint upload destination and the location of the folder of files to upload

powershell-variable-to-change-sharepoint-cameron-dwyer

5. Run the PowerShell script, you should see progress of the uploaded files as shown below

result-powershell-save-files-to-sharepoint

6. After the script completes you should see all of the imported files in your SharePoint document library

result-entire-folder-files-imported-to-sharepoint-cameron-dwyer

Related articles

How to install a DLL to the GAC on Windows Server 2012 using only PowerShell (without having to install SDK or Visual Studio)

Prior to Windows Server 2012 I had been use to installing DLL files in the Windows Global Assembly Cache (GAC) by either opening the Windows/Assembly folder in Explorer and simply dragging and dropping the file, or by using GacUtil.exe

With Windows Server 2012 unfortunately it’s not quite so easy. Being able to simply open the GAC in Explorer and drag/drop is gone. Also GacUtil.exe is not present on the server by default.

PowerShell to the rescue. Here’s how to register a DLL called “MyDLL.dll” to the GAC (and also how to remove it.

For this example assume we have the “MyDLL.dll” file stored at c:\temp\MyDLL.dll

To add a DLL to the GAC

1. Run the SharePoint PowerShell console as Administrator

2. Enter the following PowerShell

Set-location "c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("c:\temp\MyDLL.dll")
iisreset

To remove a DLL from the GAC

1. Run the SharePoint PowerShell console as Administrator

2. Enter the following PowerShell

Set-location "c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacRemove("c:\temp\MyDLL.dll")
iisreset

To view information for a DLL

Please refer to this article for how to retrieve more information for a assembly (DLL) using PowerShell including the assembly name, version, culture and public key token.

Free Webinar – Improve SharePoint End-User Adoption with OnePlaceMail

If you haven’t had the opportunity to see how OnePlaceMail has been making SharePoint users grin from ear-to-ear then I have great news. In addition to the awesome resources available on the OnePlaceMail website, the OnePlaceMail Team are now running free webinars to help you get the most out of your investment in SharePoint and most importantly get your users on side by making SharePoint a joy to use from the desktop.

The next webinar is scheduled for Wed 5 June (4-5pm EDT) and topics covered include:

– Bringing SharePoint to the desktop
– Seamless capture of content
– Streamline content classification
– SharePoint 2013 Site Mailboxes

Don’t miss out, register now for the free webinar.

For those struggling with user adoption of SharePoint, this is what happy users look like…

happy-sharepoint-users-adoption-acheived-500

%d bloggers like this: