Get all OneDrive for Business folders recursively

Brass 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

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
}

}

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' 
    }

}