Description
Summary of the new feature
Warn on Pattern: [xml](Get-Content <File>)
Suggest Pattern: $a = [xml]::new(); $a.Load(<File>)
This pattern is currently in wide use, but it has some problems and should be discouraged.
-
This pattern, due to using Get-Content, has problems if the (perfectly valid) xml file being parsed is not utf8 encoded. See: Make parsing XML from a file robustly easier, with a New-Xml cmdlet and the ability to cast file paths to [xml] PowerShell#14505 & https://stackoverflow.com/questions/65263942/how-to-load-or-read-an-xml-file-using-convertto-xml-and-select-xml/65264118#65264118
-
This pattern is very inefficient. I know that PSSA does not generally worry about this - but since the encoding issue above exists, feels worth it to mention. Testing against a ~1mb file shows
[xml](Get-Content <File>)
as ~12x slower, and against a 170mb file as 22x slower (loading took 46 seconds!).
What is the latest version of PSScriptAnalyzer at the point of writing
PSSA 1.19.1
Powershell Versions
This currently affects both Windows Powershell 5.1 and Powershell 7.