Our company is doing a migration from SharePoint sub-sites to new sites.

The only things being moved are document libraries, since we'll be moving a lot of libraries I have looked into doing this with PowerShell.

I have been using PnP PowerShell with Move-PnPFile. But I'm struggling to move an entire library worth of content. I can only get PowerShell to work for individual folders/files within the library.

I have tried to list items and loop through them to move, but I cannot seem to get this working either.

I'd like to move the library from https://companyname.sharepoint.com/IT/Test to https://companyname.sharepoint.com/sites/IT/Test

Connect-PnPOnline -Url 'https://companyname.sharepoint.com/IT' -UseWebLogin

$sourceUrl = 'https://companyname.sharepoint.com/IT/Test/'
$targetUrl = 'https://companyname.sharepoint.com/sites/IT/Test/'

$allDocs = (Get-PnPListItem -List 'Test').FieldValues

foreach ($item in $allDocs)
    Move-PnPFile -SourceUrl ($sourceUrl + $item.FileLeafRef) -TargetUrl ($targetUrl + $item.FileLeafRef) -Force

Basically this is what I am trying to achieve. I have tried all sorts of different variations of ways to get items name/path, used relative URLs, different URLs, none has worked for me. (Get-PnPListItem also lists all items, I only need top level files/folders as all sub-files will move with the folder)

I have full admin access permission to both sites, and I am a global administrator.

1 Answer 1


you can get top level files and folders like this:

$allDocs = Get-PnPFolderItem -FolderSiteRelativeUrl $yourList | Where {$_.Name -ne "Forms"}

This is how I did it. Property lists is array of my libraries:

function MoveFilesAndFoldersToAnotherSiteCollection
    foreach($list in $lists)
        $items = Get-PnPFolderItem -FolderSiteRelativeUrl $list | Where {$_.Name -ne "Forms"}
        $targetLibraryUrl = $relativeTargetUrl + '/' + $list;

        foreach($item in $items)
            Move-PnPFile -SourceUrl $item.ServerRelativeUrl -TargetUrl $targetLibraryUrl -AllowSchemaMismatch -Force -AllowSmallerVersionLimitOnDestination
            Write-Output "Moved Item: " $item.ServerRelativeUrl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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