0

We are migrating to SharePoint 2013 from DFS (File Server). I am looking for PowerShell scripts for bulk Upload and bulk Check-in script.

closed as too broad by Dikesh Gandhi, wjervis, Robert Lindgren Jan 27 '17 at 7:14

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

I've created a couple scripts addressing file server to SP migrations that I have used with success. Not the best in the world, I'm sure, but they have worked well for me.

To use them successfully, follow these steps.

  1. Create an "Upload" folder to hold all folders and files.
  2. In the Upload folder, create a folder for each site you are uploading files to. Name them the site URL, but leave off the protocol and replace "/" with "-". For example, to upload to "http://intranet.company.com/docs", name your folder "intranet.company.com-docs".
  3. In each site folder, create one or more folders named identically to the library/libraries you want to upload files to, e.g. "Documents".
  4. Place the files you wish to upload in their corresponding library folder.
  5. Run the SPFileMigrationPrep.ps1 script with appropriate environment variables.
#===============================================================#
.SYNOPSIS  
    Rename files in a directory, replacing all SharePoint invalid
    characters with a valid character and ensuring no reserved
    file prefixes or suffixes are in use in preparation for file
    migration to the SharePoint platform. A log file detailing
    operations is created for review.
.REQUIREMENTS
    PowerShell v3.0
    Modify permissions on scan directory and all child items
    Modify permissions on log directory
.INFO  
    File Name     : SPFileMigrationPrep.ps1
    Author        : Clayton Haffey
    Contact       : chaffey@thesharepoineptople.com
    Version       : 1.1
    Last Modified : 1-23-2017
#===============================================================#

######################################
### v EDIT ENVIRONMENT VARIABLES v ###

# directory containing files for scan
[string]$path = "D:\Drivers"

# directory of operation log output
[string]$logpath = "D:\Log"

# invalid character replacement char
[string]$char = "_"

# replace multiple consecutive invalid
# chars with single replacement char
[bool]$multrep = $true

# replace consecutive spaces with
# single space
[bool]$remspace = $true

### ^ EDIT ENVIRONMENT VARIABLES ^ ###
######################################

# create log file
$start = Get-Date
$logname = "file_prep_log_" + $start.ToString("MMddyyyyhhmmss") + ".txt"
$log = New-Item -Path $logpath -Name $logname -ItemType File

# regular expression to match invalid characters
$spregex = '[\~\!\#\?\*\%\&\/\+\|\\\:\\"]'

# invalid endings to check
$ends = ".files","_files","-Dateien","_fichiers","_bestanden","_file","_archivos","-filer","_tiedostot","_pliki","soubory","_elemei","_ficheiros","_arquivos","_dosyalar","_datoteke","_fitxers","_failid","_fails","_bylos","fajlovi","_fitxategiak"
$endsregex = ".files|_files|-Dateien|_fichiers|_bestanden|_file|_archivos|-filer|_tiedostot|_pliki|soubory|_elemei|_ficheiros|_arquivos|_dosyalar|_datoteke|_fitxers|_failid|_fails|_bylos|fajlovi|_fitxategiak"

# get all files with invalid characters
$files = Get-ChildItem -Path $path -File -Recurse | ? { $_.Name -match $spregex -or $_.Name.StartsWith("_") -eq $true -or $_.Name -match $endsregex }

# function creates the new file name
function GetNewFileName($directory, $name) {
    [string]$newname = [regex]::replace($name, $spregex, $char)
    $char2 = $char + $char
    If ($newname -match $char2 -and $multrep) {
        While ($newname -match $char2) {
            $newname = $newname.Replace($char2, $char)
        }
    }
    If ($newname -match "  " -and $remspace) {
        While ($newname -match "  ") {
            $newname = $newname.Replace("  ", " ")
        }
    }
    If ($newname.StartsWith("_")) {
        While ($newname.StartsWith("_")) {
            $newname = $newname.Substring(1)
        }
    }
    If ($newname -match "\.\.") {
        While ($newname -match "\.\.") {
            $newname = $newname.Replace("..", ".")
        }
    }
    $ext = "." + $newname.Split(".")[$newname.Split(".").Count - 1]
    $name_no_ext = $newname.Replace("." + $newname.Split(".")[$newname.Split(".").Count - 1], "")
    ForEach ($end in $ends) {
        if ($name_no_ext.EndsWith($end)) {
            $newname = $name_no_ext + "_" + $ext
        }
    }
    $newpath = $directory + "\" + $newname
    If (Test-Path -Path $newpath) {
        $suffix = 0
        $ext = "." + $newname.Split(".")[$newname.Split(".").Count - 1]
        $name_no_ext = $newname.Replace("." + $newname.Split(".")[$newname.Split(".").Count - 1], "")
        While (Test-Path -Path $newpath) {
            $suffix += 1
            $testname = $name_no_ext + "_" + $suffix.ToString() + $ext
            $newpath = $directory + "\" + $testname
        }
        $newname = $testname
    }
    Return [string]$newname
}

# write to log file
function LogWrite($entry) {
    Add-content $log -value $entry
}

# log start
$entry = "OPERATION START: " + $start.DateTime
LogWrite -entry $entry
$entry = " "
LogWrite -entry $entry

# iterate files and attempt rename
If ($files.Count -gt 0) {
    If ($char -match $spregex) {
        $entry = "Invalid character replacement char `"$char`" is not a valid character for SharePoint file names."
        LogWrite -entry $entry
    }
    Else {
        ForEach ($file in $files) {
            $entry = "PROCESSING: " + $file.FullName
            LogWrite -entry $entry
            Try {
                [string]$new = GetNewFileName -directory $file.DirectoryName -name $file.Name
                Rename-Item -LiteralPath $file.FullName -NewName $new
                $entry = "SUCCESS: " + $file.Name + " to " + $new
                LogWrite -entry $entry
            }
            Catch {
                $entry = "ERROR: " + $file.FullName
                LogWrite -entry $entry
                $entry = "MESSAGE: " + $_
                LogWrite -entry $entry
            }
        }
    }
}
Else {
    $entry = "No files requiring preparation found."
    LogWrite -entry $entry
}

# log end
$end = Get-Date
$entry = " "
LogWrite -entry $entry
$entry = "OPERATION END: " + $end.DateTime
LogWrite -entry $entry
$entry = "OPERATION DURATION: " + ($end - $start) -f "hh:mm:ss"
LogWrite -entry $entry

############# END SCRIPT #############
######################################
  1. Run the SPFileMigrationUpload.ps1 script with appropriate variables.
#===============================================================#
.SYNOPSIS  
    Uploads files to library and site specified via directory
    structure. Upload path should contain folders named for
    site urls, e.g. "thesharepointpeople.sharepoint.com". The
    protocol should not be included, and "/" characters in the
    url should be replaced with the "-" character. Example:
    FOLDER: "thesharepointpeople.sharepoint.com-docs"
        EQUALS
    SITE: "https://thesharepointpeople.sharepoint.com/docs"
    Directories under each site named directory should be named
    after the library in the corresponding site. Folders in
    libraries are not currently supported. Only files to be
    uploaded should be included in the library named directories.
    A log file detailing operations is created for review.
.REQUIREMENTS
    PowerShell v3.0
    Read permissions on upload directory and all child items
    Contribute permissions on relevant SharePoint libraries
.INFO  
    File Name     : SPFileMigrationUpload.ps1
    Author        : Clayton Haffey
    Contact       : chaffey@thesharepoineptople.com
    Version       : 1.0
    Last Modified : 1-23-2017
#===============================================================#

######################################
### v EDIT ENVIRONMENT VARIABLES v ###

# directory containing files for upload
[string]$path = "D:\Upload"

# directory of operation log output
[string]$logpath = "D:\Log"

# login name
[string]$user = "domain\user"

# password
[string]$pass = "password"

# Office 365 / SharePoint Online?
[bool]$O365 = $false

# SSL
[bool]$SSL = $false

### ^ EDIT ENVIRONMENT VARIABLES ^ ###
######################################

# load client assemblies
Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location)
Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location)

# create log file
$start = Get-Date
$logname = "file_upload_log_" + $start.ToString("MMddyyyyhhmmss") + ".txt"
$log = New-Item -Path $logpath -Name $logname -ItemType File

# write to log file
function LogWrite($entry) {
    Add-content $log -value $entry
}

# upload file
function UploadFile($context, $folder, $file) {
    $stream = New-Object IO.FileStream($file.FullName,[System.IO.FileMode]::Open)
    $info = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $info.Overwrite = $true
    $info.ContentStream = $stream
    $info.url = $file
    $upload = $folder.Files.Add($info)
    $context.Load($upload)
    $context.ExecuteQuery()
    If($upload.CheckOutType -ne "none") {
        $upload.CheckIn("Checked in by Administrator", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
    }
    $context.Load($upload)
    $context.ExecuteQuery()
}

# log start
$entry = "OPERATION START: " + $start.DateTime
LogWrite -entry $entry
$entry = " "
LogWrite -entry $entry

# iterate files and attempt rename
If ((Get-ChildItem -LiteralPath $path -Directory).Count -gt 0) {
    $dirs = Get-ChildItem -LiteralPath $path -Directory
    ForEach ($dir in $dirs) {
        $context = ""
        $protocol = "http://"
        If ($SSL) {
            $protocol = "https://"
        }
        $weburl = $protocol + $dir.Name.Replace("-", "/")
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($weburl)
        If ($context) {
            If ($O365) {
                $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,(ConvertTo-SecureString $pass -AsPlainText -Force))
            }
            Else {
                $Creds = New-Object System.Net.NetworkCredential($user, (ConvertTo-SecureString $pass -AsPlainText -Force))
            }
            $context.Credentials = $creds
            $libs = Get-ChildItem -LiteralPath $dir.FullName -Directory
            ForEach ($lib in $libs) {
                $list = $context.Web.Lists.GetByTitle($lib)
                $context.Load($list)
                $context.ExecuteQuery()
                $files = Get-ChildItem -LiteralPath $lib.FullName -File
                ForEach ($file in $files) {
                    Try {
                        UploadFile -context $context -folder $list.RootFolder -file $file
                        $entry = "SUCCESS: " + $file.Name + " uploaded to library " + $lib + " in site " + $weburl
                        LogWrite -entry $entry
                    }
                    Catch {
                        $entry = "ERROR: " + $file.Name + " failed to upload"
                        LogWrite -entry $entry
                        $entry = "MESSAGE: " + $_
                        LogWrite -entry $entry
                    }
                }
            }
        }
        Else {
            $entry = "ERROR: Unable to establish context for URL: " + $weburl
            LogWrite -entry $entry
        }
    }
}
Else {
    $entry = "No files to upload found."
    LogWrite -entry $entry
}

# log end
$end = Get-Date
$entry = " "
LogWrite -entry $entry
$entry = "OPERATION END: " + $end.DateTime
LogWrite -entry $entry
$entry = "OPERATION DURATION: " + ($end - $start) -f "hh:mm:ss"
LogWrite -entry $entry

############# END SCRIPT #############
######################################

Hope you find this useful!

Not the answer you're looking for? Browse other questions tagged or ask your own question.