Convert to base64 windows

Base64 Windows Command Line Conversion Utility

This is a small tool to allow for command line base64 encoding. The tool provides a number of ways to convert to and from base64 format. It’s useful for script automation and installer scripts or for simply getting base64 output from binary files for pasting etc.

Features:

  • Single file executable on Windows
  • Cross Platform dotnet tool
  • File based creation and parsing
  • base64 string encoding and decoding support
  • StdIo and StdIo input and output support
  • Mix and match input and output formats
  • Clipboard output (Windows)

The standalone EXE is windows only, while the .NET tool can work cross-platform.

Single File Binary Download and Usage

You can download the single file Base64.exe file directly from the GitHub site here:

Base64.exe Single File Exe

You can also install this tool as a Dotnet Tool with the .NET SDK 6.0 or later (cross platform):

dotnet tool update -g Westwind.Base64

Note that the Clipboard features (-c) do not work in the Dotnet Tool version at this time.

Create a Windows File Context Menu Shortcut

Recommended usage is to save the file to a location that is in your Windows path so that you can execute it from anywhere.

It’s also useful to add a Explorer File Context Menu Shortcut which lets you interactively create .b64 files from any file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Base64]
"Command"="C:\\Users\\rstrahl\\Dropbox\\admin\\Base64.exe"
@="Convert to Base64 File (.b64)"

[HKEY_CLASSES_ROOT\*\shell\Base64\command]
@="C:\\Users\\rstrahl\\Dropbox\\admin\\Base64.exe \"%1\""

Save as Base64.reg and double-click in Explorer to add to the registry after which you should see a Convert to Base64 (.b64) shortcut on the Explorer File Context Menu.

You can also set up another one to convert a file to Base64 on the clipboard which is useful for other use cases:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Base64]
"Command"="C:\\Users\\rstrahl\\Dropbox\\admin\\Base64.exe"
@="Convert to Base64 and place on Clipboard"

[HKEY_CLASSES_ROOT\*\shell\Base64\command]
@="C:\\Users\\rstrahl\\Dropbox\\admin\\Base64.exe \"%1\" -c"

Syntax

The command line syntax available is as follows:

Base64 Encoder
--------------
(c) West Wind Technologies, 2023-2025

Convert files to and from base64.

Syntax
------
Base64  encode|decode|decodetext  -i inputFile -o outputFile -c Clipboard -t Console output

Commands
--------
encode              Encode files to base64
decode              Base64 file content to binary output file
decodetext          Decode base64 text to binary output file
HELP || /?          This help display

Input and Output Files
----------------------
-i                  input file (or 2nd parameter w/o -i) (encode,decode)
                    input text  (decodeText)
-o                  output file (or 3rd parameter w/o -o)

Encoding Output
---------------
-c                  encoding output TO clipboard (Windows only)
                    decoding input FROM clipboard (Windows only)
-t                  encoding output to terminal console output
-s                  add leading space to output (allow sending Gmail)

Examples
--------

base64 test.pdf                               // creates same file with .b64 ext appended
base64 test.pdf -c -t                         // creates output to clipboard and console out
base64 encode test.pdf test.pdf.b64           // binary file -> b64 file + clipboard +Terminal
base64 decode test.pdf.b64 test_restored.pdf  // b64 file ->binary file
base64 decodetext -o test_restored.pdf -c            // from clipboard to output file
base64 decodetext -i JVBERi0xLjQKMSAwIG9iago8P== -o test_restored.pdf    // b64 text -> binary output

License

This library is published under MIT license terms.

Copyright © 2023-2025 Rick Strahl, West Wind Technologies

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the «Software»), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED «AS IS», WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  1. About Base 64

  2. Converting To and From Base64

Base64 Encoding in Windows PowerShell

Base64 encoding is one of the methods of binary-to-text encoding methods that will represent binary data to a more readable string format.

There are no Windows PowerShell-native commands for Base64 conversion (as of PowerShell [Core] 7.1). So, for now, direct use of the .NET library is needed. This article will show you the possible methods of converting to and from Base64 using Windows PowerShell and the .NET library.

About Base 64

In technical terms, Base64 encoding converts three 8-bit bytes into four 6-bit bytes, consisting of bits numbered 0-63, thus the name Base64. In addition, the decoded data is 3/4 as long as the original string syntax.

While the Base64 encoding method can encode plain text, its real benefit is encoding non-printable characters interpreted by transmitting systems as control characters.

Therefore, you must always explicitly specify what character encoding the Base64 bytes should represent.

Converting To and From Base64

On converting to Base64, you must first obtain a byte representation of the string you’re trying to encode using the character encoding the user of the Base64 string expects.

Also, on converting FROM Base64, you must interpret the resultant array of bytes as a string using the same encoding that we used to create the Base64 representation.

The following examples below will convert to and from UTF-8 encoded string formats using the .NET library:

Converting to Base64:

[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes('Motorhead'))

Output:

Converting from Base64:

[Text.Encoding]::ASCII.GetString([Convert]::FromBase64String('TW90b3JoZWFk'))

Output:

This article kept defining Base64 encoding as a series of bytes converting to an ASCII string format. However, with Windows PowerShell and the .NET library, we can directly convert to and from other string formats such as Unicode or UTF-8.

To convert to and from UTF-16LE (“Unicode”) or ASCII instead, substitute [Text.Encoding]::Unicode and [Text.Encoding]::ASCIIfor [Text.Encoding]::UTF8 respectively.

Example Syntax:

[Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("YmxhaGJsYWg="))
[Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String("YmxhaGJsYWg="))

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

Excerpt

If you want to encode or decode a string to Base64 from your command line, there are some simple steps to follow. Here are some practical ways to achieve this on different operating systems and shells.

Encoding String to Base64

Encoding in Windows

  1. Open your command prompt.

  2. Type the following command, replacing input_file and output_file with your desired inputs and outputs:

    1certutil -encode input_file output_file
    

    For example, to encode a string, you can use the following command:

    1echo -n "IToolkit" | certutil -encode -
    

Encoding in Linux

  1. Open your terminal.

  2. Type the following command, replacing input_file and output_file with your desired inputs and outputs:

    1base64 input_file -w 0 > output_file
    

    For example, to encode a string, you can use the following command:

    1echo -n "IToolkit" | base64
    

Encoding in Mac

  1. Open your terminal.

  2. Type the following command, replacing input_file and output_file with your desired inputs and outputs:

    1base64 -i input_file -o output_file
    

    For example, to encode a string, you can use the following command:

    1echo -n "IToolkit" | base64
    

Encoding in Shell

  1. Open your shell.

  2. Type the following command, replacing input_file and output_file with your desired inputs and outputs:

    1openssl enc -base64 -in input_file -out output_file
    

    For example, to encode a string, you can use the following command:

    1echo -n "IToolkit" | openssl enc -base64
    

The free Base64 Encode verification tool is as follows:

Decoding Base64 to String

Decoding in Windows

  1. Open your command prompt.

  2. Type the following command, replacing input_file and output_file with your desired inputs and outputs:

    1certutil -decode input_file output_file
    

    For example, to decode a string, you can use the following command:

    1echo -n "SVRvb2xraXQ=" | certutil -decodehex -
    

Decoding in Linux

  1. Open your terminal.

  2. Type the following command, replacing input_file and output_file with your desired inputs and outputs:

    1base64 -d input_file -w 0 > output_file
    

    For example, to decode a string, you can use the following command:

    1echo -n "SVRvb2xraXQ=" | base64 -d
    

Decoding in Mac

  1. Open your terminal.

  2. Type the following command, replacing input_file and output_file with your desired inputs and outputs:

    1base64 -D -i input_file -o output_file
    

    For example, to decode a string, you can use the following command:

    1echo -n "SVRvb2xraXQ=" | base64 -D
    

Decoding in Shell

  1. Open your shell.

  2. Type the following command, replacing input_file and output_file with your desired inputs and outputs:

    1openssl enc -base64 -d -in input_file -out output_file
    

    For example, to decode a string, you can use the following command:

    1echo -n "SVRvb2xraXQ=" | openssl enc -base64 -d
    

The free Base64 Decode verification tool is as follows:

Precautions when Using

  • Be careful with sensitive information; Base64 is not encryption.
  • Always double-check if you typed the correct syntax for your command.
  • Don’t forget to check if the output is correct.

In conclusion, encoding and decoding from Base64 in the command line is a practical skill that is useful in various contexts. By following these simple steps, you can easily encode and decode strings in Base64. Just be sure to follow the noted precautions to avoid any potential issues.

Recently, while working on an automation project, I got a requirement to encode files to Base64, a process that converts binary data into ASCII string format. I will show you here different methods to convert files to Base64 using PowerShell with examples and complete scripts.

To convert a file to Base64 in PowerShell, you can use the [System.Convert]::ToBase64String method to encode a byte array obtained from the file. For example, $base64String = [System.Convert]::ToBase64String((Get-Content -Path ‘C:\MyFolder’ -Encoding Byte)) will give you the Base64 encoded string of the file, which you can then output or use as needed.

What is Base64 Encoding?

Base64 is a binary-to-text encoding mechanism that converts binary data in an ASCII string format into a radix-64 representation. This encoded data can then be easily transmitted or stored in text-based systems. This encoding is often used when transferring data over a medium that only supports text, such as embedding binary files within scripts or sending files via email.

Convert Files to Base64 Using System.Convert Class

To convert a file to Base64 in PowerShell, you can use the .NET System.Convert class. This class provides a ToBase64String method that can be used to convert a byte array into a Base64 string.

Here’s an example script that reads the content of a file into a byte array and then converts it to a Base64 string:

$fileContent = Get-Content -Path 'C:\MyFolder\Myexe.exe -Encoding Byte
$base64String = [System.Convert]::ToBase64String($fileContent)
Set-Content -Path 'C:\MyFolder\base64.txt' -Value $base64String

In this script, Get-Content reads the file as a byte array, which is then passed to ToBase64String to perform the conversion. The resulting Base64 string is saved to a text file using Set-Content.

Convert Files to Base64 Using Base64 Encoding with Streams

For large files, it’s more efficient to use streams to avoid loading the entire file into memory. The following example demonstrates how to use .NET streams to convert a file to Base64 using PowerShell:

$filePath = 'C:\MyFolder\Myexe.exe'
$outputPath = 'C:\MyFolder\base64.txt'

$inFileStream = [System.IO.File]::OpenRead($filePath)
$base64Stream = [System.IO.MemoryStream]::new()
$base64Writer = [System.IO.StreamWriter]::new($base64Stream)

$buffer = New-Object byte[] 8192
while ($inFileStream.Read($buffer, 0, $buffer.Length)) {
    $base64Writer.Write([System.Convert]::ToBase64String($buffer))
}

$base64Writer.Flush()
$base64Stream.Position = 0
$base64String = [System.IO.StreamReader]::new($base64Stream).ReadToEnd()

Set-Content -Path $outputPath -Value $base64String

$inFileStream.Close()
$base64Stream.Close()

This script creates a file stream for reading the input file and a memory stream for writing the Base64 encoded data. It reads chunks of the file into a buffer and writes the Base64 encoded string to the memory stream, which is then written to the output file.

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

Convert Files to Base64 in PowerShell

Using a PowerShell Custom Function

To make the process reusable, you can create a custom PowerShell function that encapsulates the logic for converting a file to Base64.

Here is the complete PowerShell function.

function ConvertTo-Base64 {
    param (
        [Parameter(Mandatory=$true)]
        [string]$InputFile,
        [Parameter(Mandatory=$true)]
        [string]$OutputFile
    )

    $fileContent = Get-Content -Path $InputFile -Encoding Byte
    $base64String = [System.Convert]::ToBase64String($fileContent)
    Set-Content -Path $OutputFile -Value $base64String
}

# Usage
ConvertTo-Base64 -InputFile 'C:\MyFolder\file.exe' -OutputFile 'C:\MyFolder\base64.txt'

This function, ConvertTo-Base64, takes two parameters: the input file path and the output file path. It reads the content of the input file, encodes it to Base64, and saves it to the output file.

Conclusion

Encoding files to Base64 using PowerShell is easy by using the .NET System.Convert class.

I have also explained how to convert files to base64 using base64 encoding with Streams using PowerShell. I hope it helps.

You may also like the following PowerShell tutorials:

  • Get Newest File In Directory In PowerShell
  • Create a Password-Protected Zip File Using PowerShell
  • How to Check if a File is Empty with PowerShell?
  • How to Get File Version in PowerShell?
  • How to Get MD5 Hash of a File in PowerShell?

Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.

Unlock the potential of Base64 encoding in Windows CMD with certutil. Transform binary data to text seamlessly. Your guide to efficient data handling.

What is Base64?

Base64 is a binary-to-text encoding scheme that converts binary data into an ASCII string format, facilitating safe data transmission and storage. This encoding method represents binary information using a set of 64 characters, consisting of letters, numbers, and symbols. Base64 is widely employed in various applications, including email attachments and encoding binary files for inclusion in text-based formats like XML and JSON. It serves as a versatile solution for ensuring data integrity and compatibility across different systems and platforms.

What is Shell?

A shell is a command-line interface (CLI) software that allows users to interact with an operating system. It evaluates user input and forwards it to the operating system for execution. The term “shell” is more broad and can refer to any CLI, whether for Unix-like or Windows-based systems. Shells can have features such as scripting, variables, and the ability to automate activities via command sequences.

What is CMD?

The command-line interpreter application present in Windows operating systems is known as CMD, or Command Prompt. It provides a text-based interface via which users may interact with the system, run applications, and accomplish various tasks. CMD is a rudimentary command-line interface in comparison to more current command-line interfaces, with a limited selection of commands and features.

Base64 Encoding and Decoding in CMD

In CMD, you can use the certutil command-line tool to perform Base64 encoding and decoding. Although certutil is primarily used for managing certificates, it also includes the -encode and -decode operations, which can be used on any file, not just certificates.

Here is the syntax:

certutil <method> <inputFile> <outputFile>

This command is used to encode or decode a file using Base64 encoding or decoding with the certutil command-line tool in Windows. <method> represents either the -encode or -decode parameter, <inputFile> represents the name of the input file, and <outputFile> represents the name of the output file. The result of the encoding or decoding operation will be saved to the file specified by <outputFile>.

For example, if you want to encode a file named input.txt using Base64 encoding, you can use the following command:

certutil -encode input.txt output.b64

This will create an output.b64 file that contains the Base64 encoded contents of the input.txt file.

If you want to decode a Base64 encoded file named input.b64, you can use the following command:

certutil -decode input.b64 output.txt

This will create an output.txt file that contains the decoded contents of the input.b64 file.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Dsd player for windows 10
  • Ntuser dat что это за папка в windows 10
  • Как самому обновить драйвера на windows 10
  • Windows office для mac
  • Невозможно установить драйвер или утилиты принтера в данной системе windows