Using Powershell to Base64 Encode an MP3 Audio File

I'm trying to use Powershell to Base64 encode an mp3 audio file (for uploading via API call), but for some reason the resulting Base64 encoded text isn't right. Here is my code:

$recording="c:\myfile.mp3"
$fileContent = Get-Content $recording -Encoding UTF8
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$fileContentEncoded | set-content ("c:\output.b64")

Open in new window


I compare my resulting text from this script with the resulting text from one of the free online base64 encoders (which I know is correct), and I get a different string of text.

Can anyone help me?
garyknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
"You're reading it wrong." This is a binary file, you need to use Byte as encoding.
Encode and save:
$inFile = "c:\myfile.mp3"
$b64File = "c:\output.b64"
[System.Convert]::ToBase64String((Get-Content -Path $inFile -Encoding Byte)) | Set-Content -Path $b64File -Encoding ASCII

Open in new window

Decode and compare:
$b64File = "c:\output.b64"
$recoveredFile = "c:\myfile.b64.mp3"
[System.Convert]::FromBase64String((Get-Content -Path $b64File -Encoding ASCII -Raw)) | Set-Content -Path $recoveredFile -Encoding Byte -Force
& fc.exe $inFile $recoveredFile

Open in new window

0
garyknightAuthor Commented:
Thanks for your quick reply. I tried to encode using your method and got the following error. I'm using Powershell 6.0.4...not sure if that makes a difference.

Get-Content : Cannot bind parameter 'Encoding'. Cannot convert the "Byte" value of type "System.String" to type "System.Text.Encoding".
At C:\encoder.ps1:7 char:71
+ ... rt]::ToBase64String((Get-Content -Path $inFile -Encoding Byte)) | Set ...
+                                                              ~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetContentCommand
0
oBdACommented:
It seems like PowerShell 2.0 is about as tenacious as Windows XP ...
Seriously, PowerShell 2.0 is from 2009. PS 5.1 is even supported on Windows 7 / Server 2008 R2, and PS3.0 is already a lot better than PS 2.0. Consider upgrading.
Windows Management Framework 5.1
https://www.microsoft.com/en-us/download/details.aspx?id=54616
Encode on a legacy system:
$inFile = "c:\myfile.mp3"
$b64File = "c:\output.b64"
[IO.File]::WriteAllText($b64File, ([System.Convert]::ToBase64String(([IO.File]::ReadAllBytes($inFile)))))

Open in new window

Decode on a legacy system:
$b64File = "c:\output.b64"
$recoveredFile = "c:\myfile.b64.mp3"
[IO.File]::WriteAllBytes($recoveredFile, ([System.Convert]::FromBase64String(([IO.File]::ReadAllText($b64File)))))
& fc.exe $inFile $recoveredFile

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
@oBdA, that might be an issue with PS Core (!), not PS 2.0, as the OP stated they use 6.0.4
0
garyknightAuthor Commented:
Here is what I get from $PSVersionTable:

PS C:\Program Files\PowerShell\6.0.4> $Psversiontable

Name                           Value
----                           -----
PSVersion                      6.0.4
PSEdition                      Core
GitCommitId                    v6.0.4
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
So I have been correct. Looking at https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-6:

Beginning in PowerShell 6.0, Byte is no longer a valid option for the -Enoding parameter. You can use the -AsByteStream parameter to indicate that the content should be read and output as a byte stream.
So just replacing -Encoding Byte with -AsByteStream should be sufficient.

Edit: Just checked, you cannot do that with 5.1. You need to check for the version than, to allow different PS releases.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garyknightAuthor Commented:
Thanks to you both!!
0
footechCommented:
@Qlemo - glad you found that link, as I thought I had read something similar, but couldn't find it.  I did find this one (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/providers/filesystem-provider/get-content-for-filesystem?view=powershell-6) which has incorrect info, and the built-in help also has bad info.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
@garyknight, please also select oBdA's first comment as solution, not only as Helpful. He found the basic issue, I have been just correcting the new option as he was not aware of a change there.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
@footech, that's very bad, because the obvious location to look for is yours. No one will look into MIcrosoft Powershell Management for Get-Content  :<, and I have just been lucky to get the correct link by searching with the correct keywords.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.