Development

Base64 in Bash and PowerShell CLI

Learn how to encode and decode Base64 directly from the command line using native tools in Linux, macOS, and Windows without installing any third-party software.

Published on August 18, 20265 min read

Sometimes you need to quickly encode a string or a file for a configuration file, SSH key, or Kubernetes secret, and you don't want to open a web browser or write a Python script.

Fortunately, macOS, Linux, and Windows all include native command-line utilities for working with Base64.

Newlines Matter

When echoing strings in Bash, the echo command adds a newline character (\n) at the end of your string by default. This newline gets encoded into your Base64 output, which is usually not what you want. Always use echo -n to suppress the newline!

1. Linux & macOS (Bash/Zsh)

On UNIX-like systems, the base64 core utility is installed by default. You can pipe data into it via standard input (stdin).

Encoding Strings

Remember to use -n with echo.

bash
# Correct: Encodes exactly "hello world"
echo -n "hello world" | base64
# Output: aGVsbG8gd29ybGQ=

# Incorrect: Encodes "hello world\n"
echo "hello world" | base64
# Output: aGVsbG8gd29ybGQK

Decoding Strings

To decode, pass the --decode flag (or -d on most Linux distributions, -D on older macOS).

bash
echo -n "aGVsbG8gd29ybGQ=" | base64 --decode
# Output: hello world

Encoding and Decoding Files

You can also pass a file directly to the base64 command.

bash
# Encode a file
base64 my_secret.key > encoded.txt

# Decode a file
base64 --decode encoded.txt > decoded.key

2. Windows (PowerShell)

Windows doesn't have a native base64 executable, but PowerShell provides access to the robust .NET System.Convert class.

Encoding Strings

powershell
$text = "hello windows"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
$base64 = [System.Convert]::ToBase64String($bytes)

Write-Output $base64
# Output: aGVsbG8gd2luZG93cw==

Decoding Strings

powershell
$base64 = "aGVsbG8gd2luZG93cw=="
$bytes = [System.Convert]::FromBase64String($base64)
$text = [System.Text.Encoding]::UTF8.GetString($bytes)

Write-Output $text
# Output: hello windows

Bonus: certutil (Windows CMD)

If you are stuck in the old Windows Command Prompt (cmd.exe) and need to encode a file without PowerShell, you can use the built-in certificate utility certutil.

bash
# Encode a file to Base64
certutil -encode input.txt output.b64

# Decode a Base64 file
certutil -decode output.b64 input.txt

Don't want to deal with the terminal?

If you are wrestling with shell escaping, newlines, or formatting, just drop your text or files into our secure, client-side web converter.

Go to the Base64 Converter