Powershell with recursive looping in SharePoint Online

Implementing Recursion in PowerShell

These days all the clients are looking for migrating their data from local environment to SharePoint / Office 365 for better collaboration. As part this migration process for better progress and quick migration we need to implement some automation process. The best way(s) to migrate data from local folders / drives to SharePoint or Office 365 is:
-> PowerShell scripting
-> Implementing windows application with managed client side object model
-> ECMA script to upload files
As discussed above I would like to discuss about the first approach PowerShell scripting. To read all the files from the local drive / folder we need to make use of recursion.
Code sample for implementing recursion in PowerShell.

try {
    function getAllFile([string]$path) {
        $fc = new-object -com scripting.filesystemobject
        $folder = $fc.getfolder($path)
        Write-Host “Folder Name=”$folder.Name “Path=”$path
        foreach ($i in $folder.files) {
            Write-Host “`nFile Name::” $i.Name
            Write-Host “File Path::” $i.Path
        foreach ($i in $folder.subfolders) {
    getAllFile “G:\KVN Articles”
catch {
    Write-Host “`n Error:: $($_.Exception.Message)” -ForegroundColor Red -BackgroundColor Yellow
Let us walk through the code:
-> new-object -com scripting.filesystemobject – To get the object of the file system
-> $fc.getfolder($path) – To get object of folder
-> $folder.subfolders – To get object of folder
The output will be as follows:
Implementing Recursion in PowerShell
Implementing Recursion in PowerShell

Hope this helps.!!!

Similar SharePoint 2013 Tutorials

Leave a Reply