Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Encode and Decode Base64 in Windows Using CMD and PowerShell

Base64 is a method for encoding binary data into ASCII text. It is commonly used for encoding email attachments, embedding images in HTML, and storing complex data in XML or JSON. In the Windows environment, you can easily encode and decode Base64 using built-in tools like Command Prompt (CMD) and PowerShell.

Examples:

Encoding and Decoding Base64 with PowerShell

PowerShell provides straightforward cmdlets for handling Base64 encoding and decoding.

Encoding Text to Base64:

To encode a simple text string to Base64, you can use the following PowerShell command:

$text = "Hello, World!"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
$base64 = [Convert]::ToBase64String($bytes)
Write-Output $base64

This script converts the string "Hello, World!" into its Base64 representation.

Decoding Base64 to Text:

To decode a Base64 string back to text, use:

$base64 = "SGVsbG8sIFdvcmxkIQ=="
$bytes = [Convert]::FromBase64String($base64)
$text = [System.Text.Encoding]::UTF8.GetString($bytes)
Write-Output $text

This will convert the Base64 string back to "Hello, World!".

Encoding and Decoding Base64 with CMD

While CMD does not have native support for Base64 encoding and decoding, you can use PowerShell commands within CMD to achieve the same results. Here’s how you can do it:

Encoding Text to Base64:

powershell -Command "$text = 'Hello, World!'; $bytes = [System.Text.Encoding]::UTF8.GetBytes($text); $base64 = [Convert]::ToBase64String($bytes); Write-Output $base64"

Decoding Base64 to Text:

powershell -Command "$base64 = 'SGVsbG8sIFdvcmxkIQ=='; $bytes = [Convert]::FromBase64String($base64); $text = [System.Text.Encoding]::UTF8.GetString($bytes); Write-Output $text"

These commands allow you to perform Base64 encoding and decoding directly from the Command Prompt by leveraging PowerShell.

Conclusion

Base64 encoding and decoding are essential operations for data handling and transmission in various applications. Windows provides robust support for these operations through PowerShell, which can be easily accessed from both PowerShell and CMD environments.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.