I would like to read the content of XML files in SharePoint Online. I would like to check if the XML file contains some text. I already tried something like this but it is not working:

Connect-PnPOnline https://myCompany.sharepoint.com/teams/SomeSiteCollection
$fileRelativeURL = "/SomeDocumentLibrary/test.xml"
$file = Get-PnPFile -Url $fileRelativeURL -AsListItem
$stream = $file.OpenBinaryStream()

But the stream is empty and has no value.

enter image description here

How to get the content of a file in PowerShell and SharePoint Online?

  • Try this and let me know if it works for you. – Ganesh Sanap Jun 17 at 9:48
  • Hi @GaneshSanap, thank you. This article is SharePoint on prem. I would like to run a powershell script to SharePoint Online. – Jackson Jun 17 at 9:55
  • 1
    You can get the XML content as a string using PnP PowerShell in SP Online. check my answer below. – Ganesh Sanap Jun 17 at 9:58

To get the XML file content as a string in a variable, use below code:

Connect-PnPOnline -Url https://tenant.sharepoint.com/sites/SiteName -UseWebLogin
$fileRelativeURL = "/sites/SiteName/Shared Documents/test.xml"
$file = Get-PnPFile -Url $fileRelativeURL -AsString

Documentation: Get-PnPFile - Examples

Then you can check if the text is present in XML file string or not using:

$textToFind = "SomeTextFromFile"
$index = $file.indexOf($textToFind)

If $index is equal to -1 then the substring ($textToFind) is not found in XML file content Else it is found.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.