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,047 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,432 questions
{count} votes

Accepted answer
  1. MotoX80 32,526 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. Chris H 21 Reputation points
    2022-02-16T20:38:11.05+00:00

    Looks like my mistake was not paying close enough attention. For some reason I had inadvertently altered the SourceFolderURL path to a non-existent location during some of the testing. Once I found that and modified the code from before that MotoX80 had provided for checking the data it worked. Below is the code with a snippet of the CSV for testing for anyone else that experiences the issue.

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

    175142-csv-contents2.jpg

    0 comments No comments