How to load and read excel file hosted in sharepoint online using powershell

Benard Mwanza 1,006 Reputation points
2022-02-18T12:02:21.15+00:00

Hi all,

I'm working on a PowerShell script to read content in excel file hosted in SharePoint online in office 365.

My goal is retrieve content for cell B2 from the file.

My code so far,

    $Excel = New-Object -ComObject Excel.Application
    $Workbook = $Excel.Workbooks.Open("\\bernardcomms.sharepoint.com\sites\Test_Site\Documents\test_with_macro.xlsm")
    $Excel.Workbooks.CheckOut("\\bernardcomms.sharepoint.com\sites\Test_Site\Documents\test_with_macro.xlsm")

    # Get the first sheet in excel file 
    $workSheet = $Workbook.Sheets.Item(1)
    $WorkSheet.Name

    # Get value for cell B2
    $mailto = $workSheet.cells.Item(2, 2).Text
    $mailto.Text

On executing the script above, it just hangs. What am i doin wrong.

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. mcpyassine 226 Reputation points
    2022-07-12T12:08:16.4+00:00

    Hello,

    Try
    $mailto = $workSheet.cells.Item(2, 2)
    or
    $mailto = $workSheet.cells.Item(2, 2).Value

    If the answer is helpful, please click "Accept Answer" and kindly upvote it.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Aung Zaw Min Thwin 306 Reputation points
    2022-07-13T01:29:06.25+00:00

    Please try one of below to open workbook.

    $WorkBook = $Excel.Workbooks.Open('https://bernardcomms.sharepoint.com/sites/Test_Site/Documents/test_with_macro.xlsm')  
    

    or

    $WorkBook = $Excel.Workbooks.Open('\\bernardcomms.sharepoint.com@SSL\DavWWWRoot\sites\Test_Site\Documents\test_with_macro.xlsm')  
    
    
    
      
    
    0 comments No comments

Your answer

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