Question:
I’ve got a huge XML file (0.5 GB), with no line breaks. I want to be able to look at, say, the first 200 characters without opening the whole file. Is there a way to do this with PowerShell?
Answer:
PowerShell Desktop (up to 5.1)
You can read at the byte level with Get-Content like so:
1 2 3 |
$bytes = Get-Content .\files.txt -Encoding byte -TotalCount 200 [System.Text.Encoding]::Unicode.GetString($bytes) |
If the log file is ASCII you can simplify this to:
1 2 |
[char[]](Get-Content .\files.txt -Encoding byte -TotalCount 200) |
PowerShell Core 6.0 and newer
PowerShell Core doesn’t support byte
encoding. It’s been replaced by -AsByteStream
parameter.
1 2 3 |
$bytes = Get-Content .\file.txt -AsByteStream -TotalCount 200 [System.Text.Encoding]::Unicode.GetString($bytes) |