Fix: You Cannot Call a Method on a Null-Valued Expression

“You cannot call a method on a null-valued expression” means the thing before .Method() is $null, usually because a variable was never set or a command returned nothing. Check it first:

if ($null -ne $report) { $report.Name.ToUpper() }

In this tutorial, I’ll reproduce you cannot call a method on a null-valued expression in PowerShell 7 and 5.1, then walk through the causes and fixes.

What does “You cannot call a method on a null-valued expression” mean?

Reading a property of $null quietly returns $null, but calling a method throws this error (ID InvokeMethodOnNull). A one-letter typo triggers it:

"ErrorView: $ErrorView"
$customerName = 'Maria Lopez'
$custmerName.ToUpper()
'The script kept running after the error.'

PowerShell 7’s default ConciseView shows the file, line and failing code:

You cannot call a method on a null-valued expression in PowerShell 7 ConciseView with Line | and the failing code
ConciseView points at line 3 and the failing expression (PowerShell 7)

Windows PowerShell 5.1 uses NormalView, with “At …:3 char:1”, CategoryInfo and the error ID:

InvokeMethodOnNull error in Windows PowerShell 5.1 NormalView with CategoryInfo and FullyQualifiedErrorId
The same error in NormalView, with the error ID on the last line (Windows PowerShell 5.1)

Both kept running the next line, so a script can carry on with bad data. The PowerShell error variables keep the full record.

Common causes of the null-valued expression error

The failing line is rarely the real problem; something earlier left the variable empty. This script tries four common cases:

function Test-Call ([string]$Label, [scriptblock]$Call) {
    try { $null = & $Call; "${Label}: OK" }
    catch { "${Label}: Failed. $($_.Exception.Message)" }
}
$report = Get-ChildItem -Path . -Filter 'nullmethod-*.xlsx' | Select-Object -First 1
$notes  = Get-Content -Path .\nullmethod-missing.txt -ErrorAction SilentlyContinue
$match  = Select-String -Path .\nullmethod-orders.csv -Pattern 'Phoenix'
$order  = [pscustomobject]@{ Id = 1042; Customer = 'Dana Reyes' }

Test-Call 'No file matched ' { $report.Name.ToUpper() }
Test-Call 'File not read   ' { $notes.Split(',') }
Test-Call 'No text match   ' { $match.Line.Trim() }
Test-Call 'Missing property' { $order.ShipTo.Trim() }

Output:

No file matched : Failed. You cannot call a method on a null-valued expression.
File not read   : Failed. You cannot call a method on a null-valued expression.
No text match   : Failed. You cannot call a method on a null-valued expression.
Missing property: Failed. You cannot call a method on a null-valued expression.

All four failed the same way in 7 and 5.1. The Get-ChildItem filter matched nothing, S