PowerShell function that reads file bytes using System.IO.BinaryReady. It can read all bytes, last n bytes (-Last) or first n bytes (-First).
Pipeline compatible with System.IO.FileInfo.
-Path <string>Absolute or relative file path. AliasFullName.[-First <long>]Specifies the number of Bytes from the beginning of a file.[-Last <long>]Specifies the number of Bytes from the end of a file.[<CommonParameters>]
| Name | Type | Description |
|---|---|---|
FilePath |
String |
Absolute File Path |
Length |
Int64 |
File Length in Bytes |
Bytes |
Object[] |
Array containing the File Bytes |
- Tested and compatible with PowerShell v5.1 and PowerShell Core.
Get-ChildItem . -File | Read-Bytes -Last 100: Reads the last100bytes of all files on the current folder. If the-Lastargument exceeds the file length, it reads the entire file.Get-ChildItem . -File | Read-Bytes -First 100: Reads the first100bytes of all files on the current folder. If the-Firstargument exceeds the file length, it reads the entire file.Read-Bytes -Path path/to/file.ext: Reads all bytes offile.ext.
FilePath Length Bytes
-------- ------ -----
/home/user/Documents/test/...... 14 {73, 32, 119, 111…}
/home/user/Documents/test/...... 0
/home/user/Documents/test/...... 0
/home/user/Documents/test/...... 0
/home/user/Documents/test/...... 116 {111, 109, 101, 95…}
/home/user/Documents/test/...... 17963 {50, 101, 101, 53…}
/home/user/Documents/test/...... 3617 {105, 32, 110, 111…}
/home/user/Documents/test/...... 638 {101, 109, 112, 116…}
/home/user/Documents/test/...... 0
/home/user/Documents/test/...... 36 {65, 99, 114, 101…}
/home/user/Documents/test/...... 735 {117, 112, 46, 79…}
/home/user/Documents/test/...... 1857 {108, 111, 115, 101…}
/home/user/Documents/test/...... 77 {79, 80, 69, 78…}