I'm trying to find .nfo files that aren't XML. It seems to mostly work, but some of the files are XML and are still being listed. One example has this for the first line:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
Here is my code. I'm not sure what I am doing wrong.
Get-ChildItem "c:\*.nfo" -recurse | ForEach-Object {
if (Get-Content -LiteralPath $_ -First 1 | Select-String -Pattern 'xml' -NotMatch)
{
Write-Output $_.Fullname
}
}
I don't care if they are properly formatted XML files so I am just using the first line to test.
-LiteralPath $_->-LiteralPath $_.FullNameGet-Content, the PSPath property would be used as an alias for LiteralPath.if (-not ($_ | Get-Content -TotalCount 1).Contains('xml')). Select-String shouldn't be needed as you've already got the first line anyway, and-Firstis an alias so using the main parameter name-TotalCountmay be better practise.