Connecting to OneDrive for Business using PnP PowerShell

Loading

OneDrive for Business is a cloud storage service in Microsoft 365 that enables users to store and share files securely. PnP PowerShell provides an efficient way to manage OneDrive programmatically, allowing administrators to automate tasks like retrieving files, managing permissions, and synchronizing data.

This guide covers:
✔️ Installing and setting up PnP PowerShell
✔️ Connecting to OneDrive for Business
✔️ Running basic OneDrive commands


Step 1: Install PnP PowerShell

If you haven’t installed PnP PowerShell, install it using:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force

Once installed, import the module:

Import-Module PnP.PowerShell

PnP PowerShell is ready!


Step 2: Connect to OneDrive for Business

Use the following command to establish a connection:

Connect-PnPOnline -Url "https://yourtenant-my.sharepoint.com" -Interactive

🔹 Replace “yourtenant” with your Microsoft 365 tenant name.
🔹 A login prompt will appear. Sign in using your OneDrive for Business account.

You are now connected to OneDrive!


Step 3: Verify Connection

To confirm the connection, run:

Get-PnPContext

If successfully connected, you’ll see site details, including the OneDrive URL.


Step 4: Retrieve OneDrive Files

To list all files in your OneDrive:

Get-PnPListItem -List "Documents"

🔹 The default OneDrive library is named “Documents”.
🔹 This command retrieves all files and folders in the root folder.

OneDrive files retrieved!


Step 5: Upload a File to OneDrive

To upload a file to OneDrive:

$localFilePath = "C:\Users\YourUser\Desktop\sample.txt"
$oneDriveFolder = "Documents"

Add-PnPFile -Path $localFilePath -Folder $oneDriveFolder

🔹 Replace "sample.txt" with your actual file name and path.
🔹 The file will be uploaded to OneDrive’s “Documents” folder.

File uploaded to OneDrive!


Step 6: Download a File from OneDrive

To download a file from OneDrive:

$oneDriveFilePath = "Documents/sample.txt"
$localSavePath = "C:\Users\YourUser\Downloads\sample.txt"

Get-PnPFile -ServerRelativeUrl $oneDriveFilePath -Path $localSavePath -AsFile

🔹 Replace “sample.txt” with the file you want to download.
🔹 The file will be saved locally.

File downloaded successfully!


Step 7: Delete a File from OneDrive

To delete a file from OneDrive:

$oneDriveFilePath = "Documents/sample.txt"

Remove-PnPFile -ServerRelativeUrl $oneDriveFilePath -Recycle

🔹 This moves the file to the OneDrive recycle bin instead of permanent deletion.
🔹 To permanently delete, remove -Recycle.

File deleted from OneDrive!


Step 8: List and Manage OneDrive Permissions

To list permissions for a file or folder:

$oneDriveFilePath = "Documents/sample.txt"

Get-PnPListItemPermission -List "Documents" -Identity $oneDriveFilePath

This shows who has access to a specific OneDrive file or folder.

Permissions retrieved!


Step 9: Disconnect from OneDrive

When finished, disconnect from OneDrive:

Disconnect-PnPOnline

Disconnected successfully!

Leave a Reply

Your email address will not be published. Required fields are marked *