PowerShell
# Import PnP PowerShell module (assuming it's already installed)
Import-Module PnP.PowerShell
# Connect to SharePoint site using app registration or certificate authentication (replace with your details)
Connect-PnPOnline -Url <Your_SharePoint_Site_Url> -ClientId <App_Client_ID> -ClientSecret <App_Client_Secret>
# Define local file path and SharePoint library location
$localFilePath = "C:\Path\To\Your\Document.docx"
$destinationFolder = "/Shared Documents" # Replace with target folder path (or omit for root library)
# Upload the file using Add-PnPFile cmdlet
Add-PnPFile -Path $localFilePath -Folder $destinationFolder -Values @{"Title" = "MyUploadedDocument.docx"}
# Optional: Confirmation message
Write-Host "Document uploaded successfully!"
Use code with caution.content_copy
Explanation:
- Import Module: The script starts by importing the PnP PowerShell module, assuming it’s already installed.
- Connect to SharePoint: Use
Connect-PnPOnline
to establish a connection to your SharePoint site. You’ll need to replace placeholders with your actual SharePoint site URL, app client ID, and client secret (for app registration authentication). - Define Paths: Set the
localFilePath
variable with the path to your document on your local machine. Define thedestinationFolder
variable specifying the target folder within the SharePoint library (use a site-relative URL). If omitted, the file uploads to the library root. - Upload the File: The core functionality lies in the
Add-PnPFile
cmdlet. It takes three arguments:Path
: Path to the local file.Folder
: Site-relative URL of the target folder (or omit for root library).Values
(optional): A hashtable containing metadata for the uploaded file. Here, we set the “Title” property to “MyUploadedDocument.docx”.
- Confirmation Message: (Optional) A
Write-Host
statement displays a confirmation message upon successful upload.