msgraph-sdk-powershell
msgraph-sdk-powershell copied to clipboard
Need documentation for New-MgDriveItem
Trying to upload files to OneDrive Useing New-MgDriveItem -DriveId "" -Name "test.csv" -FileMimeType "text/csv" -ContentInputFile "C:\example.csv" and receive following error
The property 'content' does not exist on type 'oneDrive.item'. Make sure to only use property names that are defined by the type. AB#7438
Unfortunately this is one of those commands that is a little "different" and the autogeneration fails to do the right thing. It will need some love.
Related to #54
I'm trying to upload a CSV to OneDrive using New-MgDriveItem -DriveId "b!lHY...ZZ" -Name "test.csv" -ContentInputFile "C:\Temp\test.csv" and I'm seeing the error:

same issue here, is there a solution or workaround?
Workaround for small files under 4MB:
Invoke-GraphRequest -Method PUT -Uri '/v1.0/drives/{drive-id}/items/root:/{name-of-file.ext}:/content' -InputFilePath c:\path\to\name-of-file.ext
For a SPO site's default document library:
Invoke-GraphRequest -Method PUT -Uri '/v1.0/sites/{site-id}/drive/items/root:/{name-of-file.ext}:/content' -InputFilePath c:\path\to\name-of-file.ext
Large upload session is a different story. Maybe after 2.5 years, finally time to show these cmdlets "some love"?
Uploading new/existing files to OneDrive is already supported via Set-MgDriveItemContent:
Set-MgDriveItemContent -DriveId $DriveId -DriveItemId "root:/myFile.txt:" -InFile "PathToFile.txt"
See https://github.com/microsoftgraph/msgraph-sdk-powershell/pull/433.
As for /sites/{site-id}/drive/items/root:/{name-of-file.ext}:/content, the path is currently missing in the OpenAPI document used to generate the module. I've opened an issue in the metadata repo for a fix - https://github.com/microsoftgraph/msgraph-metadata/issues/238.
Two things to report
- Failure:
Set-MgDriveItemContent : Entity only allows writes with a JSON Content-Type header.... when using:
$Site = Get-MGSite -SiteId "mydomain.sharepoint.com:/sites/sitetest-20210825"
$DefaultDrive = Get-MgSiteDefaultDrive -SiteId $Site.id
Set-MgDriveItemContent -DriveId $DefaultDrive.Id -DriveItemId "root:/myfile.txt" -InFile "C:\Users\Robin\Downloads\upload.txt"
Though I could absolutely be doing something wrong here. I've failed to find anything online to help with this, so clutching at straws.
- The page for
New-MgDriveItemis so big that it basically hung my browser for at least 30 seconds 😶
I'm using Microsoft.Graph.Files version 1.20.
Ah, it was a missing :
❌ -DriveItemId "root:/myfile.txt"
✅ -DriveItemId "root:/myfile.txt:"
Super easy to miss. FWIW, for a time I was confused by the parameter name (-DriveItemId as it'd suggest needing the id of something in existence) but I appreciate the difficulty in naming things when it comes to determining file names, file paths, and complete paths.
[DriveItemId <String>]: key: id of driveItem on the docs doesn't aid understanding. Examples would be nice too 🙏🏼
@robinmalik @peombwa Hi guys. I don't suppose you could share some layman's terms level tips for using the large/resumable upload sessions? I find New-MgDriveItemUploadSession, but nothing else that obviously goes with it.
I really can't tell if Set-MgDriveItemContent is meant for use with New-MgDriveItemUploadSession, or if they are each their own entire thing. The parameters for New-MgDriveItemUploadSession have me wishfully thinking that it is a one-stop-shop cmdlet and that I can provide my item to the -Item parameter.
Any guidance will be much appreciated.
@JeremyTBradshaw are we meant to use Invoke-WebRequest? this is how I see it done here... (ref. the last several lines of code)
@mikewoodd3432 thanks for that and yes, exactly that. I just so happened to be looking into this this week and came to realize that's the way to 'feed' the data. That link will come in handy for reference for sure, thanks again.
I got a file uploaded to SharePoint @JeremyTBradshaw (throughput was slow....)
I used New-MgDriveItemChild to create an empty item in a drive folder (New-MgDriveItem would be similar) # tell the method I want a file item, by providing an empty IMicrosoftGraphFile structure for the -File parameter $IMicrosoftGraphFile = @{ hashes = "" mimeType = "" } # create the new file item $newitem = New-MgDriveItemChild -DriveId $drive.Id -DriveItemId $drivefolder.Id -Name $uploadFilePath -File $IMicrosoftGraphFile
Then used $driveUploadSession as per the Invoke-WebRequest method as per web link I mentioned previously. # create the upload session $driveUploadSession = New-MgDriveItemUploadSession -DriveId $drive.Id -DriveItemId $newitem.Id
Thanks @mikewoodd3432 . Glad to see live human confirmation that it works. I plan to use this stuff in this script: https://github.com/JeremyTBradshaw/PowerShell/blob/main/Export-MSGraphMailbox.ps1
Just waiting for the right level of business in my day to day to really tackle it, hopefully soon.
That's what worked for me. Courtesy of GitHub Copilot. 😊
# Connect to Microsoft Graph
Connect-MgGraph -TenantId $tenantId -ClientId $appId -Scopes "Files.ReadWrite.All"
# SharePoint Drive Id (Document Library)
$driveId = ""
# File to upload
$filePath = "C:\Temp\test.csv"
$fileProperties = Get-ChildItem -Path $filePath
# Read the file content as a byte array
$fileContent = [System.IO.File]::ReadAllBytes($filePath)
# Destination file name
$destinationName = "$($fileProperties.BaseName)-$(Get-Date -Format "yyyy-MM-dd HH-mm-ss")$($fileProperties.Extension)"
# Upload the file to SharePoint
Invoke-MgGraphRequest -Method PUT -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/root:/IntuneHashes/$destinationName`:/content" -Body $fileContent -ContentType "application/octet-stream"