Using a CSV file to move files with Powershell on Sharepoint Online

Chris H 21 Reputation points
2022-02-11T16:03:20.623+00:00

I am not a Powershell master nor am I a newbie. I am trying to use powershell with a CSV file to move 600 or so folders from one folder to another folder in a Sharepoint Online Document library. The code I have below gives a File not found error. Not sure what I'm missing, I verified it can read the CSV file.

$SiteURL = "https://***.sharepoint.com/sites/SharedFiles/"
$SourceFolderURL= "Shared Files/Warehouse"
$TargetFolderURL = "Shared Files/Test1"
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
Import-Csv C:\Cisco\Test.csv | foreach{Move-PnPFolder -Folder $SourceFolderURL/$_.folderName -TargetFolder $TargetFolderURL}

Any help would be appreciated.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,789 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,545 questions
{count} votes

Accepted answer
  1. MotoX80 34,346 Reputation points
    2022-02-16T15:28:00.493+00:00

    A couple of things.... When you post code, please use the Code Sample icon to format it. That's the 101010 icon. Otherwise the web site removes certain characters and treats some as formatting character.

    Don't do this..

    ForEach($_ in $a)
    

    The "dollar underscore" is the pipeline variable. While that code might work, I would recommend using your own variable name to avoid confusion.

    ForEach($SiteName in $a)
    

    It looks like you are doing a copy/paste of a multiline script into a PS window. Don't do that either. Save the script as a .ps1 file and run it. Or load the code into Powershell_ISE and run it from there.

    3 days ago, you apparently had a working script that verified that the source folder existed. All you needed to do was add one more line to execute the Move_PnPFolder cmdlet. Did you do that? Did you get an error?

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. MotoX80 34,346 Reputation points
    2022-02-11T22:05:57.117+00:00

    I don't have much Sharepoint experience, but when I'm having trouble debugging PS code, I like to display the contents of the variables that I reference. And use cmdlet's like Test-Path to see if folders exit. Can you do that with Get-PNPFolder?

    You don't have quotes around $SourceFolderURL/$_.folderName so I'm not sure how that is going to be interpreted. You may have to code it as I did here.

    $SiteURL = "https://***.sharepoint.com/sites/SharedFiles/"
    $SourceFolderURL= "Shared Files/Warehouse"
    $TargetFolderURL = "Shared Files/Test1"
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
    Import-Csv C:\Cisco\Test.csv | foreach{
        "Foldername = {0}" -f $_.folderName
        $SrcName = "$SourceFolderURL/$($_.folderName)"
        "Src name   = {0}" -f $SrcName 
        Get-PnPFolder -Folder $SrcName 
    }
    
    0 comments No comments

  2. Chris H 21 Reputation points
    2022-02-12T16:23:58.347+00:00

    Here is the output:

    PS C:\WINDOWS\system32> Import-Csv C:\Cisco\Test.csv | foreach{

    > "Foldername = {0}" -f $.folderName
    > $SrcName = "$SourceFolderURL/$($
    .folderName)"
    > "Src name = {0}" -f $SrcName
    > Get-PnPFolder -Folder $SrcName
    > }

    Foldername = Test2
    Src name = Shared Files/Warehouse/Test2
    Get-PnPFolder : A parameter cannot be found that matches parameter name 'Folder'.
    At line:5 char:20

    • Get-PnPFolder -Folder $SrcName
    • ~~~~~~~
    • CategoryInfo : InvalidArgument: (:) [Get-PnPFolder], ParameterBindingException
    • FullyQualifiedErrorId : NamedParameterNotFound,PnP.PowerShell.Commands.Files.GetFolder

    Foldername = Test4
    Src name = Shared Files/Warehouse/Test4
    Get-PnPFolder : A parameter cannot be found that matches parameter name 'Folder'.
    At line:5 char:20

    • Get-PnPFolder -Folder $SrcName
    • ~~~~~~~
    • CategoryInfo : InvalidArgument: (:) [Get-PnPFolder], ParameterBindingException
    • FullyQualifiedErrorId : NamedParameterNotFound,PnP.PowerShell.Commands.Files.GetFolder

  3. Emily Du-MSFT 47,216 Reputation points Microsoft Vendor
    2022-02-15T03:30:07.073+00:00

    @Chris H

    Here're steps.

    1.Create a csv file as following picture shows. In the csv file, you need to fill in URLs of 600 folders that needs to be moved on each line.

    174290-1.png

    2.Please run below PowerShell.

    $SiteURL = "https://tenant.sharepoint.com/sites/emilytest2"  
    $TargetFolderURL = "doc1"  
    $filePath = “C:\Users\spsetup\Documents\url1.csv”  
    
    $csv = Import-Csv $filePath  
    $a = $csv.SourceFolderURL  
    
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  
    
    ForEach($_ in $a) {  
    Move-PnPFolder -Folder $_ -TargetFolder $TargetFolderURL      
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Chris H 21 Reputation points
    2022-02-15T17:05:38.4+00:00

    Emily,

    Here is the code I entered (made changes for environment) and the following error:

    PS C:\WINDOWS\system32> $SiteURL = "https://***.sharepoint.com/sites/SharedFiles/"
    PS C:\WINDOWS\system32> $TargetFolderURL = "Test1"
    PS C:\WINDOWS\system32> $filePath = "C:\Cisco\Test.csv"
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32> $csv = Import-Csv $filePath
    PS C:\WINDOWS\system32> $a = $csv.SourceFolderURL
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32> Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

    cmdlet Get-Credential at command pipeline position 1
    Supply values for the following parameters:
    Credential
    PS C:\WINDOWS\system32>
    PS C:\WINDOWS\system32> ForEach($_ in $a) {

    > Move-PnPFolder -Folder $_ -TargetFolder $TargetFolderURL
    > }

    Move-PnPFolder : File Not Found.
    At line:2 char:6

    • Move-PnPFolder -Folder $_ -TargetFolder $TargetFolderURL
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : WriteError: (:) [Move-PnPFolder], ServerException
    • FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.MoveFolder
      Move-PnPFolder : File Not Found.
      At line:2 char:6
    • Move-PnPFolder -Folder $_ -TargetFolder $TargetFolderURL
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : WriteError: (:) [Move-PnPFolder], ServerException
    • FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.MoveFolder

    CSV file contents:

    174477-csv-contents.jpg

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.