Share via

Powershell export to Sharepoint

Handian Sudianto 7,141 Reputation points
2026-01-06T01:03:45.8733333+00:00

I have powershell script to query some data and export the result to the excel file and store to local disk.

Now i have requirement to store the result to sharepoint site, it's possible to powershell to store the result to sharepoint site?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Harry Phan 20,055 Reputation points Independent Advisor
    2026-01-06T10:09:37.73+00:00

    Yes! Store the PowerShell output directly to SharePoint Online using modern authentication. The two reliable approaches are PnP.PowerShell (simplest) or Microsoft Graph (best for automation/service principals). With PnP.PowerShell, export your data to an .xlsx on disk as you already do, then connect and upload:

    Install once: Install-Module PnP.PowerShell $siteUrl  = "https://contoso.sharepoint.com/sites/Finance"

    $library  = "Shared Documents/Reports"   # Library/Folder path

    $local    = "C:\Reports\query-result.xlsx"

    Connect-PnPOnline -Url $siteUrl -Interactive   # MFA-supported sign-in

    Add-PnPFile -Path $local -Folder $library -OverwriteIfAlreadyExists

    If you need unattended runs, use Graph with an app registration that has Files.ReadWrite.All or Sites.Selected, grant site access, and then upload to the library’s drive:

    Install once: Install-Module Microsoft.Graph Connect-MgGraph -Scopes "Files.ReadWrite.All"

    $site   = Get-MgSite -Filter "displayName eq 'Finance'"

    $drive  = Get-MgSiteDrive -SiteId $site.Id | Where-Object {$_.Name -eq "Documents"}

    $bytes  = [System.IO.File]::ReadAllBytes("C:\Reports\query-result.xlsx")

    New-MgDriveItemContent -DriveId $drive.Id -DriveItemId "root:/Reports/query-result.xlsx:/content" -BodyParameter $bytes -ContentType "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

    Avoid legacy WebDAV mapping or basic auth; they’re brittle and deprecated. Ensure your account or app has at least Contribute permissions on the target library and confirm library settings such as required checkout or mandatory metadata, which can block uploads. If you prefer CSOM, you can use Microsoft.SharePointOnline.CSOM with SharePointOnlineCredentials, but favor PnP or Graph for MFA and app-based auth. If you need me to adapt your existing script, share how you generate the Excel, your tenant URL, target library/folder, and whether this runs interactively or on a scheduled task.

    I hope you've found something useful here. If it helps you get more insight into the issue, it's appreciated to accept the answer. Should you have more questions, feel free to leave a message. Have a nice day!

    Harry

    Was this answer helpful?


  2. AI answer

    2026-01-06T01:03:54.6433333+00:00

    Yes, it is possible to store the results of your PowerShell script directly to a SharePoint site. You can use the Add-PnPFile cmdlet from the SharePoint PnP PowerShell module to upload files to a SharePoint document library. Here’s a general approach to do this:

    1. Install the SharePoint PnP PowerShell Module if you haven't already:
         Install-Module SharePointPnPPowerShell
      
    2. Connect to your SharePoint site:
         Connect-PnPOnline -Url https://yourtenant.sharepoint.com/sites/yoursite -UseWebLogin
      
    3. Upload the Excel file to SharePoint:
         Add-PnPFile -Path "C:\path\to\your\file.xlsx" -Folder "Shared Documents"
      
      Replace C:\path\to\your\file.xlsx with the path to your local file and Shared Documents with the name of the document library or folder in SharePoint where you want to store the file.

    This method allows you to automate the process of uploading files to SharePoint as part of your PowerShell script.

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.