Get all OneDrive for Business folders recursively

%3CLINGO-SUB%20id%3D%22lingo-sub-44172%22%20slang%3D%22en-US%22%3EGet%20all%26nbsp%3BOneDrive%20for%20Business%20folders%20recursively%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-44172%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20litte%20problem.%20I'm%20migrating%26nbsp%3BOneDrives%20between%20Office%20365%20tenants.%20In%20the%20receiving%20end%20they%26nbsp%3Brequire%20that%20all%20documents%20have%20a%20major%20version.%20In%20the%20orginating%20end%20there%20are%20a%20few%20users%20that%20have%20turned%20on%20Minor%20versions%20i%20the%20versioning%20settings.%20This%20requires%20me%20to%20check-out%20and%20check-in%20files%20to%20raise%20all%20files%20to%20major%20version.%20.%3C%2FP%3E%3CP%3EUsing%20Office%20365%20Dev%20PnP%20PowerShell%20CmdLets%20(SharePointPNPPowerShellOnline)%20I%20created%20this%20little%20nifty%20function.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EFunction%20checkin-allfiles%20%7B%3CBR%20%2F%3E%20%5Bcmdletbinding()%5D%3CBR%20%2F%3E%3CBR%20%2F%3E%20param%20(%3CBR%20%2F%3E%20%5BParameter%20(position%20%3D%200%2CHelpMessage%20%3D%20'The%20Folder%20Site%20Relative%20URL%20(%2FDocuments)'%2C%20Mandatory%20%3D%20%24TRUE)%5D%3CBR%20%2F%3E%20%5BString%5D%24FSRU%3CBR%20%2F%3E%20)%3CBR%20%2F%3E%20%24files%20%3D%20Get-PnPFolderItem%20-FolderSiteRelativeUrl%20%24FSRU%20-ItemType%20File%3CBR%20%2F%3E%20foreach%20(%24file%20in%20%24files)%20%7B%3CBR%20%2F%3E%20Set-PnPFileCheckedOut%20-Url%20%24file.ServerRelativeUrl%20-Verbose%3CBR%20%2F%3E%20Set-PnPFileCheckedIn%20-Url%20%24file.ServerRelativeUrl%20-CheckinType%20MajorCheckIn%20-Comment%20'Checked%20In%20with%20Script%20to%20raise%20version%20to%20major'%20-Verbose%3CBR%20%2F%3E%20%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20problem%20is%26nbsp%3Brecursively%20running%20this%20function%20on%20all%20subfolders.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20anyone%20help%20me%20out%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-44172%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFolders%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ERecursion%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-44503%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20all%26nbsp%3BOneDrive%20for%20Business%20folders%20recursively%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-44503%22%20slang%3D%22en-US%22%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3EHere%20is%20my%20final%26nbsp%3Bfunction%3A%3C%2FP%3E%3CPRE%3EFunction%20Checkin-Allfiles%20%7B%0A%20%20%20%20%26lt%3B%23%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20.SYNOPSIS%0A%20%20%20%20%20%20%20%20%20%20%20%20This%20function%20checks%20performs%20a%20CheckOut%2FCheckIn%20on%20files%20in%20order%20to%20elevate%20the%20files%20to%20Major%20Version.%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20.DESCRIPTION%0A%20%20%20%20%20%20%20%20%20%20%20%20This%20function%20checks%20performs%20a%20CheckOut%2FCheckIn%20on%20files%20in%20order%20to%20elevate%20the%20files%20to%20Major%20Version.%0A%20%20%20%20%20%20%20%20%20%20%20%20It%20requires%20the%20Office%20Dev%20PnP%20module%20SharePointPnPPowerShellOnline%20to%20be%20loaded%20at%20runtime.%20%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20.PARAMETER%20LibraryUrl%0A%20%20%20%20%20%20%20%20%20%20%20%20Specifies%20url%20of%20the%20site%20collection%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20.PARAMETER%20Folder%0A%20%20%20%20%20%20%20%20%20%20%20%20Specifies%20the%20Root%20Folder%20to%20check%20in%20files%20from%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20.EXAMPLE%0A%20%20%20%20%20%20%20%20%20%20%20%20Checkin-Allfiles%20-LibraryUrl%20https%3A%2F%2Fcontoso.sharepoint.com%20-Folder%20Documents%20%0A%20%20%20%20%20%20%20%20%20%20%20%20This%20gets%20all%20files%20in%20the%20documents%20folder%20and%20all%20subfolders%2C%20and%20checks%20in%20a%20new%20major%20version%20of%20all%20files%20%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20.EXAMPLE%0A%20%20%20%20%20%20%20%20%20%20%20%20Checkin-Allfiles%20-LibraryUrl%20https%3A%2F%2Fcontoso.sharepoint.com%20-Folder%20Documents%2Fsubfolder%20%0A%20%20%20%20%20%20%20%20%20%20%20%20This%20This%20gets%20all%20files%20in%20the%20Subfolder%20folder%20and%20all%20subfolders%2C%20and%20checks%20in%20a%20new%20major%20version%20of%20all%20files%20%0A%0A%0A%0A%20%20%20%20%23%26gt%3B%0A%20%20%20%20%5Bcmdletbinding()%5D%0A%0A%20%20%20%20param%20(%0A%20%20%20%20%20%20%20%20%5BParameter%20(position%20%3D%200%2CHelpMessage%20%3D%20'The%20Document%20Library%20URL'%2C%20Mandatory%20%3D%20%24TRUE)%5D%0A%20%20%20%20%20%20%20%20%5BString%5D%24LibraryUrl%2C%0A%20%20%20%20%20%20%20%20%5BParameter%20(position%20%3D%201%2CHelpMessage%20%3D%20'The%20Folder%20Site%20Relative%20URL%20(Documents)'%2C%20Mandatory%20%3D%20%24TRUE)%5D%0A%20%20%20%20%20%20%20%20%5BString%5D%24Folder%0A%20%20%20%20)%0A%20%20%20%20Write-Verbose%20-Message%20%22Connecting%20to%20%24LibraryUrl%22%0A%20%20%20%20Connect-PnPOnline%20-Credentials%20(get-credential)%20-url%20%24LibraryUrl%0A%20%20%20%20Write-Verbose%20-Message%20%22Getting%20the%20files%20in%20%24Folder%22%20%0A%20%20%20%20%24files%20%3D%20Find-PnPFile%20-Folder%20%24Folder%20-Match%20*%20-Verbose%0A%20%20%20%20Write-Verbose%20-Message%20'Done%20getting%20files'%0A%20%20%20%20foreach%20(%24file%20in%20%24files)%20%7B%0A%20%20%20%20%20%20%20%20Write-Verbose%20-Message%20%22Checking%20out%20%24(%24file.ServerRelativeUrl)%22%0A%20%20%20%20%20%20%20%20Set-PnPFileCheckedOut%20-Url%20%24file.ServerRelativeUrl%20%0A%20%20%20%20%20%20%20%20Write-Verbose%20-Message%20%22Checking%20in%20%24(%24file.ServerRelativeUrl)%22%0A%20%20%20%20%20%20%20%20Set-PnPFileCheckedIn%20-Url%20%24file.ServerRelativeUrl%20-CheckinType%20MajorCheckIn%20-Comment%20'Checked%20In%20with%20Script%20to%20raise%20version%20to%20major'%20%0A%20%20%20%20%7D%0A%0A%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-44470%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20all%26nbsp%3BOneDrive%20for%20Business%20folders%20recursively%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-44470%22%20slang%3D%22en-US%22%3E%3CP%3EUse%20%22Find-PnPFile%22%20instead%20of%20Get-PnPFolderItem%20and%20try.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EFunction%20checkin-allfiles%20%7B%0A%5Bcmdletbinding()%5D%0A%0Aparam%20(%0A%5BParameter%20(position%20%3D%200%2CHelpMessage%20%3D%20'The%20Folder%20Site%20Relative%20URL%20(%2FDocuments)'%2C%20Mandatory%20%3D%20%24TRUE)%5D%0A%5BString%5D%24FSRU%0A)%0A%24files%20%3D%20Find-PnPFile%20-Folder%20%24FSRU%20-Match%20*%0Aforeach%20(%24file%20in%20%24files)%20%7B%0A%0ASet-PnPFileCheckedOut%20-Url%20%24file.ServerRelativeUrl%20-Verbose%0ASet-PnPFileCheckedIn%20-Url%20%24file.ServerRelativeUrl%20-CheckinType%20MajorCheckIn%20-Comment%20'Checked%20In%20with%20Script%20to%20raise%20version%20to%20major'%20-Verbose%0A%7D%0A%0A%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

I have a litte problem. I'm migrating OneDrives between Office 365 tenants. In the receiving end they require that all documents have a major version. In the orginating end there are a few users that have turned on Minor versions i the versioning settings. This requires me to check-out and check-in files to raise all files to major version. .

Using Office 365 Dev PnP PowerShell CmdLets (SharePointPNPPowerShellOnline) I created this little nifty function.

 

Function checkin-allfiles {
[cmdletbinding()]

param (
[Parameter (position = 0,HelpMessage = 'The Folder Site Relative URL (/Documents)', Mandatory = $TRUE)]
[String]$FSRU
)
$files = Get-PnPFolderItem -FolderSiteRelativeUrl $FSRU -ItemType File
foreach ($file in $files) {
Set-PnPFileCheckedOut -Url $file.ServerRelativeUrl -Verbose
Set-PnPFileCheckedIn -Url $file.ServerRelativeUrl -CheckinType MajorCheckIn -Comment 'Checked In with Script to raise version to major' -Verbose
}

}

 

My problem is recursively running this function on all subfolders.

 

Can anyone help me out?

 

 

 

 

2 Replies
Highlighted

Use "Find-PnPFile" instead of Get-PnPFolderItem and try.

 

Function checkin-allfiles {
[cmdletbinding()]

param (
[Parameter (position = 0,HelpMessage = 'The Folder Site Relative URL (/Documents)', Mandatory = $TRUE)]
[String]$FSRU
)
$files = Find-PnPFile -Folder $FSRU -Match *
foreach ($file in $files) {

Set-PnPFileCheckedOut -Url $file.ServerRelativeUrl -Verbose
Set-PnPFileCheckedIn -Url $file.ServerRelativeUrl -CheckinType MajorCheckIn -Comment 'Checked In with Script to raise version to major' -Verbose
}

}
Highlighted

Thanks.

Here is my final function:

Function Checkin-Allfiles {
    <#   
            .SYNOPSIS
            This function checks performs a CheckOut/CheckIn on files in order to elevate the files to Major Version.
    
            .DESCRIPTION
            This function checks performs a CheckOut/CheckIn on files in order to elevate the files to Major Version.
            It requires the Office Dev PnP module SharePointPnPPowerShellOnline to be loaded at runtime. 

            .PARAMETER LibraryUrl
            Specifies url of the site collection

            .PARAMETER Folder
            Specifies the Root Folder to check in files from

            .EXAMPLE
            Checkin-Allfiles -LibraryUrl https://contoso.sharepoint.com -Folder Documents 
            This gets all files in the documents folder and all subfolders, and checks in a new major version of all files 

            .EXAMPLE
            Checkin-Allfiles -LibraryUrl https://contoso.sharepoint.com -Folder Documents/subfolder 
            This This gets all files in the Subfolder folder and all subfolders, and checks in a new major version of all files 



    #>
    [cmdletbinding()]

    param (
        [Parameter (position = 0,HelpMessage = 'The Document Library URL', Mandatory = $TRUE)]
        [String]$LibraryUrl,
        [Parameter (position = 1,HelpMessage = 'The Folder Site Relative URL (Documents)', Mandatory = $TRUE)]
        [String]$Folder
    )
    Write-Verbose -Message "Connecting to $LibraryUrl"
    Connect-PnPOnline -Credentials (get-credential) -url $LibraryUrl
    Write-Verbose -Message "Getting the files in $Folder" 
    $files = Find-PnPFile -Folder $Folder -Match * -Verbose
    Write-Verbose -Message 'Done getting files'
    foreach ($file in $files) {
        Write-Verbose -Message "Checking out $($file.ServerRelativeUrl)"
        Set-PnPFileCheckedOut -Url $file.ServerRelativeUrl 
        Write-Verbose -Message "Checking in $($file.ServerRelativeUrl)"
        Set-PnPFileCheckedIn -Url $file.ServerRelativeUrl -CheckinType MajorCheckIn -Comment 'Checked In with Script to raise version to major' 
    }

}