Creating Byte[] in PowerShell
There are probably even more ways, but these are the ones I can think of:
Direct array initialization:
[byte[]] $b = 1,2,3,4,5
$b = [byte]1,2,3,4,5
$b = @([byte]1,2,3,4,5)
$b = [byte]1..5
Create a zero-initialized array
$b = [System.Array]::CreateInstance([byte],5)
$b = [byte[]]::new(5) # Powershell v5+
$b = New-Object byte[] 5
$b = New-Object -TypeName byte[] -Args 5
And if you ever want an array of byte[]
(2-D array)
# 5 by 5
[byte[,]] $b = [System.Array]::CreateInstance([byte],@(5,5)) # @() optional for 2D and 3D
[byte[,]] $b = [byte[,]]::new(5,5)
Additionally:
# 3-D
[byte[,,]] $b = [byte[,,]]::new(5,5,5)
[byte[,]] $b = [System.Array]::CreateInstance([byte],5,5,5)
In PS 5.1, this:
[System.Byte[]]::CreateInstance(<Length>)
didn't work for me. So instead I did:
new-object byte[] 4
which resulted in an empty byte[4]:
0
0
0
0
This answer is for the question with no context. I'm adding it because of search results.
[System.Byte[]]::CreateInstance([System.Byte],<Length>)