Author Posts

May 16, 2017 at 4:03 pm

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
    {
        Add-PSSnapin Microsoft.SharePoint.PowerShell
    }
    #Script settings
#Intent is to copy multiple Folders/Subfolders/Documents from a FileShare to the appropriate document libraries in SharePoint Online
#For Ex - FileShare called Store Documents -> copy content -> SharePoint document library called Store Documents
#For Ex - FileShare called HR Managers-> copy content -> SharePoint document library called HR Managers
#Below script only tries to do one FileShare at a time can i do it for all FileShares all at once and is there a way to outout the success and failures when succeeded or failed
#This script only copies Files now and doesn't copy Folders.
	
    $webUrl = "https://abc365.sharepoint.com/sites/store0007"
    $docLibraryName = "Store Documents" 
    $docLibraryUrlName = "Store Documents"    # specify your subfolder url here
    $localFolderPath = "C:\Test" #I will input the UNC Path to FileShares
    #Open web and library
    $web = Get-SPWeb $webUrl
    write-host $webUrl
    $docLibrary = $web.Lists[$docLibraryName]
    write-host $docLibrary
    $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
    write-host $files
    ForEach($file in $files)
    {

if($file.Name.Contains(".pdf"))
{
    write-host $file

        #Open file
        try
        {
        $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();
        }
        catch
        {
        Write "Error: $file.name: $_" >>c:\logfile.txt
            continue;
        }
}
    }

    #Dispose web

    $web.Dispose()

May 17, 2017 at 3:59 pm

@Don Jones – Maybe I was not clear this script works only for Files within the File Share and doesn't do Folders, can you please assist.
I have a File Share called \\abc\store123\home and it has below folders

Store Documents
HR Documents
Videos

All these needs to be copied to the document libraries of that Store SharePoint Site in o365

Store Documents (Document Library)
HR Documents (Document Library)
Videos (Document Library)

May 17, 2017 at 8:55 pm

#I need help as this script only Copies files within the Folder and not the Folder itself



#Specify tenant admin and site URL
$User = "john.doe@abc365.onmicrosoft.com"
$SiteURL = "https://abc365.sharepoint.com/sites/store#"
$Folder = "\\0.0.0.0\Store Documents"  #UNC Path where Store Documents is a folder i am trying to copy
$DocLibName = "Store Documents" #This is the final destination where the Folder/SubFolders/Files should be moved

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

#Upload file
#Foreach ($File in (dir $Folder -File))
Foreach ($File in (Get-ChildItem $Folder -File -Recurse))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
$FileStream.Close()
}