Q:

Decode Base64 String in Powershell

I have a Base64 encoded file and I am trying the following to decode it:
[System. Text. Encoding]::ASCII.GetString([System. Convert]::FromBase64String($data)) 

However it generates the following error:
Exception calling "FromBase64String" with "1" argument(s): "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding 
characters, or an illegal character among the padding characters. 

Although it is Base64, completely.  Any ideas? 
*Please note that it is Base64, but not encoded by PowerShell. 

A:

If you would like to decode a Base64 string to text this will do:
$EncodedText = 'SGVsbG8gV29ybGQh'
$DecodedText = [System. Text. Encoding]::UTF8.GetString([System. Convert]::FromBase64String($EncodedText))
$DecodedText

If you need to decode a Base64 string to a binary file this will do:
$base64string = ''
$FileName = "file.png"
[IO.File]::WriteAllBytes($FileName, [Convert]::FromBase64String($base64string))

