wonea
wonea

Reputation: 4969

Add-Content bytes fails with PowerShell Core

I'm using PowerShell Core v6.0.2, and trying to write out an array of bytes to a file. This works fine in regular PowerShell but fails with PowerShell Core

$jsonstr = Get-Content $inputfilename
$jsonfile = ConvertFrom-Json $jsonstr
$bytes = [Convert]::FromBase64String($jsonfile.data)

$outputfilename = "test.xlsx";

Add-Content -Path $outputfilename -Value $bytes -Encoding Byte

Error:

enter image description here

Is this is a bug or can Byte no longer be used because of binary ordering issues?

Upvotes: 1

Views: 592

Answers (1)

wonea
wonea

Reputation: 4969

According to this blog post, on PowerShell Core you need to use Set-Content with the AsByteStream parameter.

I've changed my script to the following:

$jsonstr = Get-Content $inputfilename
$jsonfile = ConvertFrom-Json $jsonstr
$bytes = [Convert]::FromBase64String($jsonfile.data)

$outputfilename = "test.xlsx";
Set-Content -Path $outputfilename -Value $bytes -AsByteStream

Upvotes: 1

Related Questions