Invoke-Formatter not working correctly for small scriptblocks
Before submitting a bug report:
- Make sure you are able to repro it on the latest released version
- Perform a quick search for existing issues to check if this bug has already been reported
Steps to reproduce
Invoke-Formatter -ScriptDefinition '{ $_.Status.IsCompleted -eq $true }'
Invoke-Formatter -ScriptDefinition '{ $null -ne $_.Status }'
Expected behavior
No error.
Actual behavior
Error
Invoke-Formatter : Object reference not set to an instance of an object.
At line:1 char:1
+ Invoke-Formatter -ScriptDefinition '{ $null -ne $_.Status }; '
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Formatter], NullReferenceException
+ FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.1
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.0
1.19.1
1.19.1
(Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() } 1.19.0 1.19.1 1.19.1
It looks like this might be using 1.19.0. Can you try verifying that this occurs in 1.19.1?
It doesn't matter

Can you share the full stack trace? Either from Get-Error or $error[0] | fl * -force
writeErrorStream : True
PSMessageDetails :
Exception : System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.IsPreviousTokenOnSameLine(LinkedListNode`
1 lparen)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.<FindInnerBraceViolations>d__49.MoveNext(
)
at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.AnalyzeScript(Ast ast, String fileName)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass83_1.<AnalyzeSyntaxTree>b__2()
TargetObject :
CategoryInfo : InvalidOperation: (:) [Invoke-Formatter], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}
https://github.com/PowerShell/PSScriptAnalyzer/blob/f71cad7719607be77d7c498a07b7314fc753fa8d/Rules/UseConsistentWhitespace.cs#L598-L601
Closing due to inactivity
Please reopen. It's still broken.
It looks like the problem still exists in 1.20.0
PS F:\> Get-Command -Module PSScriptAnalyzer
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-ScriptAnalyzerRule 1.20.0 PSScriptAnalyzer
Cmdlet Invoke-Formatter 1.20.0 PSScriptAnalyzer
Cmdlet Invoke-ScriptAnalyzer 1.20.0 PSScriptAnalyzer
PS F:\> Invoke-Formatter -ScriptDefinition '{ $_.Status.IsCompleted -eq $true }'
Invoke-Formatter: Object reference not set to an instance of an object.
{ $_.Status.IsCompleted -eq $true }
PSVersionTable
Name Value
---- -----
PSVersion 7.2.4
PSEdition Core
GitCommitId 7.2.4
OS Microsoft Windows 10.0.22621
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Get Error
PS F:\> Get-Error
Exception :
Type : System.NullReferenceException
TargetSite :
Name : IsPreviousTokenOnSameLine
DeclaringType : Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace
MemberType : Method
Module : Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll
Message : Object reference not set to an instance of an object.
Source : Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
HResult : -2147467261
StackTrace :
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.IsPreviousTokenOnSameLine(LinkedListNode`1 lparen)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.FindInnerBraceViolations(TokenOperations tokenOperations)+MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.SparseArrayBuilder`1.ReserveOrAdd(IEnumerable`1 items)
at System.Linq.Enumerable.ConcatNIterator`1.LazyToArray()
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.AnalyzeScript(Ast ast, String fileName)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass82_1.<AnalyzeSyntaxTree>b__2()
CategoryInfo : InvalidOperation: (:) [Invoke-Formatter], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
InvocationInfo :
MyCommand : Invoke-Formatter
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : 77
Line : Invoke-Formatter -ScriptDefinition '{ $_.Status.IsCompleted -eq $true }'
PositionMessage : At line:1 char:1
+ Invoke-Formatter -ScriptDefinition '{ $_.Status.IsCompleted -eq $true …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Invoke-Formatter
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :
PS F:\> $error[0] | fl * -force
PSMessageDetails :
Exception : System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.IsPreviousTokenOnSameLine(LinkedListNode`1 lparen)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.FindInnerBraceViolations(TokenOperations tokenOperations)+MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.SparseArrayBuilder`1.ReserveOrAdd(IEnumerable`1 items)
at System.Linq.Enumerable.ConcatNIterator`1.LazyToArray()
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseConsistentWhitespace.AnalyzeScript(Ast ast, String fileName)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass82_1.<AnalyzeSyntaxTree>b__2()
TargetObject :
CategoryInfo : InvalidOperation: (:) [Invoke-Formatter], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}
Thanks for the reminder, I opened a PR with a fix for it