• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Comprehensive IT Guides for Pros and Enthusiasts

MENUMENU
  • HOME
  • ALL TOPICS
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Tools
    • Virtualization
    • Web
    • Windows
  • ABOUT
  • SUBSCRIBE
Home » Base64 Encode with Fixed Line Length Output in PowerShell

Base64 Encode with Fixed Line Length Output in PowerShell

March 10, 2021 - by Zsolt Agoston - last edited on March 19, 2021

Here is an example script that encodes an arbitrary text with Base64 method using PowerShell and breaks up the output into fixed-length lines to make it more easily readable. We use 76 character long rows as the MIME (Multipurpose Internet Mail Extensions) standard suggests.

The Original Text

# Our original text stored in $MyText
$MyText = @("
Recipe from: *https://www.crazyforcrust.com/best-chocolate-chip-cookie-recipe/

What makes a great Chocolate Chip Cookie Recipe?

Chocolate Chip Cookies: when you find a good one, it’s the unicorn of dessert recipes. There are so many recipes out there and I’m not gonna lie: most of them are meh.
Some of them are okay. Some of them are not very good at all. Some of them a great…but not OMG! And the rest are just…meh.

THIS chocolate chip cookie recipe is the only one (of two) that you need. It’s soft and chewy, full of chocolate, with depth of flavor. They’re easy to make with no weird ingredients and you can change up the recipe to make any kind of cookie recipe you want.

I know, I know. I already have a BOMB-DIGGITTY chocolate chip cookie recipe. I do. It’s a copycat of that Doubletree recipe everyone loves and believe me, that recipe is TOP NOTCH. But it’s not a…regular chocolate chip cookie.
")

Encoding and Splitting

# Base64 encode our text, storing the output in $b64
$b64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($MyText))

# Splitting up $b64 into 76 char long entries. The result is an array that we convert to a plain string. That step can be omitted if an array is what you need
$SplitString = $b64 -Split '(.{76})' | ? {$_} | Out-String

Output

Let's check how our encoded and encoded-split string looks like

# Only encoded, the output is a single long string:
PS C:\> $b64
ClJlY2lwZSBmcm9tOiAqaHR0cHM6Ly93d3cuY3Jhenlmb3Jj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gcmVjaXBl
IHlvdSB3YW50LgoKSSBrbm93LCBJIGtub3cuIEkgYWxyZWFkeSBoYXZlIGEgQk9NQi1ESUdHSVRU
WSBjaG9jb2xhdGUgY2hpcCBjb29raWUgcmVjaXBlLiBJIGRvLiBJdOKAmXMgYSBjb3B5Y2F0IG9m
IHRoYXQgRG91YmxldHJlZSByZWNpcGUgZXZlcnlvbmUgbG92ZXMgYW5kIGJlbGlldmUgbWUsIHRo
YXQgcmVjaXBlIGlzIFRPUCBOT1RDSC4gQnV0IGl04oCZcyBub3QgYeKApnJlZ3VsYXIgY2hvY29s
YXRlIGNoaXAgY29va2llLgo=

Reader Interactions

Comments Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  • The Original Text
  • Encoding and Splitting
  • Output

  • Terms of Use
  • Disclaimer
  • Privacy Policy
Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}