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 ClJlY2lwZSBmcm9tOiAqaHR0cHM6Ly93d3cuY3Jhenlmb3JjcnVzdC5jb20vYmVzdC1jaG9jb2xhdGUtY2hpcC1jb29raWUtcmVjaXBlLwoKV2hhdCBtYWtlcyBhIGdyZWF0IENob2NvbGF0ZSBDaGlwIENvb2tpZSBSZWNpcGU/CgpDaG9jb2xhdGUgQ2hpcCBDb29raWVzOiB3aGVuIHlvdSBmaW5kIGEgZ29vZCBvbmUsIGl04oCZcyB0aGUgdW5pY29ybiBvZiBkZXNzZXJ0IHJlY2lwZXMuIFRoZXJlIGFyZSBzbyBtYW55IHJlY2lwZXMgb3V0IHRoZXJlIGFuZCBJ4oC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