web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Automate
Suggested Answer

Run Powershell Script

(0) ShareShare
ReportReport
Posted on by 64

I have a Powershell script that runs without Error on the Desktop, however when I try to use the PAD action - 'Run a Powershell Script' to run that same script I get the following error message: : An error occurred: You cannot call a method on a null-valued expression.

Any suggestions on how to get this action to run?

 

 

This is my code: 


# Write the cell value to a text file

$cellValue | Out-File -FilePath "FILEPATH GOES HERE"

try {

    # Create an Excel COM object

    $excel = New-Object -ComObject Excel.Application

    # Open the workbook

    $workbook = $excel.Workbooks.Open("FILEPATH GOES HERE.xlsx")

    # Check if the workbook is null

    if ($workbook -eq $null) {

        throw "Failed to open the workbook."

    }

    # Get the first worksheet

    $worksheet = $workbook.Sheets.Item(1)

    # Check if the worksheet is null

    if ($worksheet -eq $null) {

        throw "Failed to get the worksheet."

    }

    # Get the value of the cell at row 1, column 1

    $cellValue = $worksheet.Cells.Item(1, 1).Value()

    # Output the cell value

    Write-Output "Cell Value: $cellValue"

    # Close the workbook without saving

    $workbook.Close($false)

}

catch {

    Write-Error "An error occurred: $_"

}

finally {

    # Quit Excel

    if ($excel -ne $null) {

        $excel.Quit()

    }

    # Release COM objects

    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null

    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null

    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

    [System.GC]::Collect()

    [System.GC]::WaitForPendingFinalizers()

}

 

 

 

This is my Error Message:
An error occurred: You cannot call a method on a 
null-valued expression.
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,04n2btij40n.tmp.ps1
 
Exception calling "Quit" with "0" argument(s): "Unable to cast COM object of type 
'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. 
This operation failed because the QueryInterface call on the COM component for the interface with IID 
'{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Element not found. (Exception from 
HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND))."
+         $excel.Quit()
+         ~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidCastException
 
Exception calling "ReleaseComObject" with "1" argument(s): "Object reference not set to an instance of an object."
+     [System.Runtime.Interopservices.Marshal]::ReleaseComObject($works ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException
 
Exception calling "ReleaseComObject" with "1" argument(s): "Object reference not set to an instance of an object."
+     [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workb ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException
 
 

I have the same question (0)
  • Suggested answer
    VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at
    Hey hi,
     
    Try this code once.
     
    try {
        # Define the file path
        $filePath = "C:\Path\To\Your\File.xlsx"
        if (-Not (Test-Path $filePath)) {
            throw "Excel file not found: $filePath"
        }
        # Create an Excel COM object
        $excel = New-Object -ComObject Excel.Application
        if ($null -eq $excel) {
            throw "Failed to create Excel COM object. Ensure Excel is installed."
        }
        # Open the workbook
        $workbook = $excel.Workbooks.Open($filePath)
        if ($null -eq $workbook) {
            throw "Failed to open the workbook: $filePath"
        }
        # Get the first worksheet
        $worksheet = $workbook.Sheets.Item(1)
        if ($null -eq $worksheet) {
            throw "Failed to get the first worksheet."
        }
        # Get the value of the cell at row 1, column 1
        $cellValue = $worksheet.Cells.Item(1, 1).Text
        Write-Output "Cell Value: $cellValue"
        # Write to a text file
        $outputFile = "C:\Path\To\Output.txt"
        $cellValue | Out-File -FilePath $outputFile -Encoding UTF8
        # Close the workbook without saving
        $workbook.Close($false)
        $excel.Quit()
    }
    catch {
        Write-Error "An error occurred: $_"
    }
    finally {
        # Release COM objects safely
        if ($worksheet) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null }
        if ($workbook) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null }
        if ($excel) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null }
        
        [System.GC]::Collect()
        [System.GC]::WaitForPendingFinalizers()
    }
     
     
    Regards,
    Vishnu Reddy
  • MCubedMama Profile Picture
    64 on at
    Thank you for your reply.
    I did run the following script and I received the following error message:
     
    C:\Users\XXXX\AppData\Local\Temp\tddslwhjvae.tmp.ps1 : An error occurred: You cannot call a method on a 
    null-valued expression.
        + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
        + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,tddslwhjvae.tmp.ps1
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 525 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 324 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard