powershell to get the first x MB of a file

2020-03-19 03:55发布

Its pretty straightforward to get the first x lines of a text file, but I need the first x MB of a file. Given that I'm a PowerShell neophyte if you could please give me some annotations on the script that would be greatly appreciated.

标签: powershell
2条回答
霸刀☆藐视天下
2楼-- · 2020-03-19 04:16
Get-Content foo.exe -TotalCount 1MB -Encoding byte

If you happen to be using PowerShell Community Extensions then do this:

Format-Hex foo.exe -count 1MB

Note: If you are using PowerShell V2 be sure to grab the 1.2 beta of PSCX.

查看更多
▲ chillily
3楼-- · 2020-03-19 04:25

Independent of PowerShell, I'd just use the head command. (It's a standard Unix command, but it's available for Windows.) The -c option lets you specify the number of bytes you want.

head -c 1024 foo.exe
查看更多
登录 后发表回答