Code Explainer

SharePoint Online File Transfer PowerShell Script

This PowerShell script automates the movement of files between libraries in SharePoint Online using the PnP PowerShell module. It covers setup for source and destination URLs, user authentication, and file transfer, enabling efficient


Empty image or helper icon

Prompt

# PowerShell Script for Moving Files in SharePoint Online

# Set source and destination URLs
$sourceURL = "https://yourtenant.sharepoint.com/sites/sourceSite/SourceLibrary"
$destURL = "https://yourtenant.sharepoint.com/sites/destinationSite/DestinationLibrary"

# Credentials and Connection
$credentials = Get-Credential
Connect-PnPOnline -Url $sourceURL -Credentials $credentials

# Move files from source to destination
Get-PnPListItem -List "SourceLibrary" | ForEach-Object {
    Move-PnPFile -ServerRelativeUrl $_.FieldValues.FileRef -TargetUrl $destURL -Force
}

Answer

PowerShell Script for Moving Files in SharePoint Online

This PowerShell script is designed to facilitate the movement of files between different libraries in SharePoint Online. Each component of the script plays a crucial role in enabling a seamless file transfer process.

Code Breakdown

1. Set Source and Destination URLs

$sourceURL = "https://yourtenant.sharepoint.com/sites/sourceSite/SourceLibrary"
$destURL = "https://yourtenant.sharepoint.com/sites/destinationSite/DestinationLibrary"
  • Purpose: This section initializes the source and destination URLs for the SharePoint libraries.
  • Components:
    • $sourceURL: The URL pointing to the library from which files are to be moved.
    • $destURL: The URL pointing to the target library for the file transfer.

2. Credentials and Connection

$credentials = Get-Credential
Connect-PnPOnline -Url $sourceURL -Credentials $credentials
  • Purpose: This part manages user authentication and establishes a connection to SharePoint Online.
  • Components:
    • Get-Credential: Prompts the user to input their SharePoint credentials (username and password), which are stored in the $credentials variable.
    • Connect-PnPOnline: Uses the specified $sourceURL and the captured credentials to connect to SharePoint Online using the PnP PowerShell module.

3. Move Files from Source to Destination

Get-PnPListItem -List "SourceLibrary" | ForEach-Object {
    Move-PnPFile -ServerRelativeUrl $_.FieldValues.FileRef -TargetUrl $destURL -Force
}
  • Purpose: This segment retrieves the files from the source library and moves them to the destination library.
  • Components:
    • Get-PnPListItem -List "SourceLibrary": Fetches all items (files) from the specified source library.
    • ForEach-Object {}: This is a loop that processes each item retrieved by Get-PnPListItem.
      • $_: Represents the current item in the pipeline.
      • Move-PnPFile: Command that moves each file.
        • -ServerRelativeUrl $_.FieldValues.FileRef: Specifies the server-relative URL of the file to be moved.
        • -TargetUrl $destURL: Indicates the destination URL where the file will be moved.
        • -Force: Overwrites the target file if it already exists.

Key Concepts

PnP PowerShell Module

  • The SharePoint PnP PowerShell module is a set of cmdlets that simplify management of SharePoint Online. It provides commands specifically designed for SharePoint and is essential for performing operations like connecting to a site and moving files.

Server-Relative URL

  • In SharePoint, a server-relative URL is a path to a resource that does not include the domain name. This is critical for commands that interact with resources within SharePoint.

ForEach-Object

  • This cmdlet allows for the iteration over a collection of objects, executing a script block for each item. It is fundamental for batch-processing tasks, such as moving multiple files.

Additional Examples

Example: Moving Specific Files

If only specific files are to be moved, you could modify the filter in Get-PnPListItem:

Get-PnPListItem -List "SourceLibrary" | Where-Object { $_.FieldValues.FileLeafRef -like "*.txt" } | ForEach-Object {
    Move-PnPFile -ServerRelativeUrl $_.FieldValues.FileRef -TargetUrl $destURL -Force
}
  • Purpose: This example specifically moves only .txt files from the source library to the destination library.

Conclusion

This PowerShell script offers a straightforward method for moving files within SharePoint Online utilizing the PnP PowerShell module. Understanding each component's functionality aids in employing and modifying this script to suit various SharePoint management needs.

For further learning on PowerShell and SharePoint management, the Enterprise DNA Platform offers extensive courses and resources aimed at enhancing your skills in data handling and automation.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This PowerShell script automates the movement of files between libraries in SharePoint Online using the PnP PowerShell module. It covers setup for source and destination URLs, user authentication, and file transfer, enabling efficient file management.