Microsoft Graph Deep Dive Series — Part 3 — OneDrive

This quick reference guide is designed for SharePoint system administrators who are new to Microsoft Graph PowerShell. Each section contains simple, practical commands that you can use immediately to manage OneDrive for Business.

OneDrive Operations

Required Permissions : Files.Read , Files.ReadWrite , Files.Read.All , Files.ReadWrite.All

All following command lines are used to manage connected user Onedrive (OneDrive of connected user) , If you want to manage other users OneDrive, you need to Register Entra ID Application and to connect through this Entra Id App.

List OneDrive items

# Connect with required permissions
Connect-MgGraph -Scopes "Files.ReadWrite.All", "User.Read.All"

# Get user DriveId
$DriveId = (Get-MgUserDrive -UserId "user@domain.com" | where Name -EQ "OneDrive" ).Id

# Get user's OneDrive root items
Get-MgUserDriveRootChild -UserId "user@domain.com" -DriveId $DriveId | Select-Object Name, Size, LastModifiedDateTime

Get OneDrive information

# Get user DriveId
$DriveId = (Get-MgUserDrive -UserId "user@domain.com" | where Name -EQ "OneDrive" ).Id

# Get OneDrive information
$Drive = Get-MgUserDrive -UserId "user@domain.com" | where Name -EQ "OneDrive"
Write-Host "Drive Type: $($Drive.DriveType)"
Write-Host "Total Space: $([math]::Round($Drive.Quota.Total / 1GB, 2)) GB"
Write-Host "Used Space: $([math]::Round($Drive.Quota.Used / 1GB, 2)) GB"
Write-Host "Remaining: $([math]::Round($Drive.Quota.Remaining / 1GB, 2)) GB"

Upload file to OneDrive

# Get user DriveId
$DriveId = (Get-MgUserDrive -UserId "user@domain.com" | where Name -EQ "OneDrive" ).Id

# Upload a file to OneDrive
$FilePath = "C:\temp\document.txt"
$FileName = Split-Path $FilePath -Leaf
Set-MgUserDriveRootChildContent -UserId "user@domain.com" -DriveId $DriveId -DriveItemId $FileName -InFile $FilePath

Download file from OneDrive

# Get user DriveId
$DriveId = (Get-MgUserDrive -UserId "user@domain.com" | where Name -EQ "OneDrive" ).Id

# Download a file from OneDrive
$DriveItem = Get-MgUserDriveRootChild -UserId "user@domain.com" -DriveId $DriveId -Filter "name eq 'document.txt'"
Get-MgUserDriveItemContent -UserId "user@domain.com" -DriveId $DriveId -DriveItemId $DriveItem.Id -OutFile "C:\temp\Doc-downloaded.txt"

Create new simple Folder in OneDrive

# Get user DriveId
$DriveId = (Get-MgUserDrive -UserId "aymen@globalitnow.com" | WhereObject Name -EQ "OneDrive").Id
$params = @{
    name = "Project Documents"
    folder = @{ x = "x" } # dummy property to force serialization of 'folder'
    "@microsoft.graph.conflictBehavior" = "rename" # "fail" |"replace" | "rename"
}
New-MgUserDriveRootChild -UserId "aymen@globalitnow.com" -DriveId $DriveId -BodyParameter $params

Create Nested Folders in OneDrive

# Prerequisite: Connect-MgGraph -Scopes "Files.ReadWrite.All" (orequivalent)
# Variables
$UserId = "user@domain.com"

# DriveId required
$DriveId = (Get-MgUserDrive -UserId $UserId | Where-Object Name -eq "OneDrive").Id

# Create nested folder structure under the root

# Create Parent Folder
$parentBody = @{
    name = "Projects" 
    folder = @{ x = "x" } # force serialization of the 'folder' facet
    "@microsoft.graph.conflictBehavior" = "rename"
}
$ParentFolder = New-MgUserDriveRootChild -UserId $UserId -DriveId $DriveId -BodyParameter $parentBody

# Create the subfolder
$subBody = @{
    name = "2025"
    folder = @{ x = "x" }
    "@microsoft.graph.conflictBehavior" = "rename"
}
# Create Nested Folders
$SubFolder = New-MgUserDriveItemChild -UserId $UserId -DriveId $DriveId -DriveItemId $ParentFolder.Id -BodyParameter $subBody

Move files inside OneDrive

Move a file ‘document.txt‘ into ‘Projects‘ folder :

# Move a file 'document.txt' into 'Projects'
$SourceFileName = "document.txt"
$DestinationFolder = "Projects"

# Get the file from root children
$FileToMove = Get-MgUserDriveRootChild -UserId $UserId -DriveId
$DriveId | where Name -EQ $SourceFileName | Select-Object -First 1

# Get the Destination folder from root children
$Destination = Get-MgUserDriveRootChild -UserId $UserId -DriveId $DriveId | where Name -EQ $DestinationFolder | Where-Object { $_.Folder } | Select-Object -First 1

# Perform the move
if ($Destination)
{
    $moveBody = @{ parentReference = @{ id = $Destination.Id } }
    Update-MgUserDriveItem -UserId $UserId -DriveId $DriveId -
    DriveItemId $FileToMove.Id -BodyParameter $moveBody
}

Copy files inside OneDrive

In this example we’ll copy “Project1.doc” to Projects folder

# Copy a file 'document.txt' into 'Projects'
$SourceFileName = "Project1.docx"
$DestinationFolder = "Projects"

# Get the file from root children
$FileToCopy = Get-MgUserDriveRootChild -UserId $UserId -DriveId
$DriveId | where Name -EQ $SourceFileName | Select-Object -First 1

# Get the Destination folder from root children
$Destination = Get-MgUserDriveRootChild -UserId $UserId -DriveId $DriveId | where Name -EQ $DestinationFolder | Where-Object { $_.Folder } | Select-Object -First 1
# Perform the copy
if ($Destination)
{
    $moveBody = @{ parentReference = @{ id = $Destination.Id }}
    Copy-MgUserDriveItem -UserId $UserId -DriveId $DriveId -
    DriveItemId $FileToCopy.Id -BodyParameter $moveBody
}

Copy Folder to another folder in OneDrive

In this example we will copy “Project Notes” Folder to this folder path : “Root/Projects/2025/01”

# Variables
$UserId = "aymen@globalitnow.com"
$SourceFolder = "Project Notes"
$DestinationFolder = "Projects/2025/01" # Destination Folder Path

# DriveId required
$DriveId = (Get-MgUserDrive -UserId $UserId | Where-Object Name -eq "OneDrive").Id

# Resolve the source folder at the root by path
$Source = Invoke-MgGraphRequest -Method GET -Uri "v1.0/users/$UserId/drive/root:/$SourceFolder"# Resolve the Destination folder at the root by path
$Target = Invoke-MgGraphRequest -Method GET -Uri "v1.0/users/$UserId/drive/root:/$DestinationFolder"

# Copy source folder to destination folder
Copy-MgUserDriveItem -UserId $UserId -DriveId $DriveId -DriveItemId $Source.id -ParentReference @{ driveId = $DriveId; id = $Target.id } -Name $Source.name

Thanks

Aymen EL JAZIRI (Microsoft MVP)
Aymen EL JAZIRI (Microsoft MVP)

Hi, I’m Aymen El Jaziri , a passionate System Administrator and Microsoft MVP, with years of hands-on experience in managing and securing modern IT infrastructures.
This blog is where I share technical guides, automation scripts, product reviews, and real-world solutions that help IT professionals simplify their day-to-day work and stay ahead in a fast-evolving cloud ecosystem.
Whether you’re here to troubleshoot an issue, improve your automation game, or learn new best practices , welcome in my blog !
Let’s build a stronger, smarter IT community together.
Feel free to connect with me on LinkedIn for more content, discussions, or collaboration opportunities.

Thanks

Aymen

Articles: 154