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

OpenTechTips

Practical IT Guides, Expert Tips, and Real-World Solutions

MENUMENU
  • HOME
  • ALL TOPICS
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Tools
    • Virtualization
    • Web
    • Windows
  • ABOUT
  • SUBSCRIBE
Home ยป Split a String Into Fixed Length Lines in PowerShell

Split a String Into Fixed Length Lines in PowerShell

March 9, 2021 - by Zsolt Agoston - last edited on July 27, 2026

# Split a long string into 50 character long rows
$NiceResult = $LongString -Split '(.{50})' | ? {$_}

Example

In this example we split up a long Base64 encoded string into 76 char long pieces. This way we not only comply with the MIME (Multipurpose Internet Mail Extensions) standard but having multiple, shorter lines make our code/content more easily readable.

PS C:\> $b64 = "RXZlcnlvbmUgbmVlZHMgYSBjbGFzc2ljIGNob2NvbGF0ZSBjaGlwIGNvb2tpZSByZWNpcGUgaW4gdGhlaXIgcmVwZXJ0b2lyZSwgYW5kIHRoaXMgaXMgbWluZS4gSXQgaXMgc2VyaW91c2x5IHRoZSBCZXN0IENob2NvbGF0ZSBDaGlwIENvb2tpZSBSZWNpcGUgRXZlciEgSSBoYXZlIGJlZW4gbWFraW5nIHRoZXNlIGZvciBtYW55LCBtYW55IHllYXJzIGFuZCBldmVyeW9uZSB3aG8gdHJpZXMgdGhlbSBhZ3JlZXMgdGhleeKAmXJlIG91dC1vZi10aGlzLXdvcmxkIGRlbGljaW91cyEKClBsdXMsIHRoZXJl4oCZcyBubyBmdW5ueSBpbmdyZWRpZW50cywgbm8gY2hpbGxpbmcsIGV0Yy4gSnVzdCBhIHNpbXBsZSwgc3RyYWlnaHRmb3J3YXJkLCBhbWF6aW5nbHkgZGVsaWNpb3VzLCBkb3VnaHkgeWV0IHN0aWxsIGZ1bGx5IGNvb2tlZCwgY2hvY29sYXRlIGNoaXAgY29va2llIHRoYXQgdHVybnMgb3V0IHBlcmZlY3RseSBldmVyeSBzaW5nbGUgdGltZSEKClRoZXNlIGFyZSBldmVyeXRoaW5nIGEgY2hvY29sYXRlIGNoaXAgY29va2llIHNob3VsZCBiZS4gQ3Jpc3B5IGFuZCBjaGV3eS4gRG91Z2h5IHlldCBmdWxseSBiYWtlZC4gUGVyZmVjdGx5IGJ1dHRlcnkgYW5kIHN3ZWV0Lg=="

# Split the string into an array with 76 char long elements
PS C:\> $SplitString = $b64 -Split '(.{76})' | ? {$_}

# To get a string output after splitting the string
PS C:\> $SplitString = $b64 -Split '(.{76})' | ? {$_} | Out-String

# Output
PS C:\> $SplitString

RXZlcnlvbmUgbmVlZHMgYSBjbGFzc2ljIGNob2NvbGF0ZSBjaGlwIGNvb2tpZSByZWNpcGUgaW4g
dGhlaXIgcmVwZXJ0b2lyZSwgYW5kIHRoaXMgaXMgbWluZS4gSXQgaXMgc2VyaW91c2x5IHRoZSBC
ZXN0IENob2NvbGF0ZSBDaGlwIENvb2tpZSBSZWNpcGUgRXZlciEgSSBoYXZlIGJlZW4gbWFraW5n
IHRoZXNlIGZvciBtYW55LCBtYW55IHllYXJzIGFuZCBldmVyeW9uZSB3aG8gdHJpZXMgdGhlbSBh
Z3JlZXMgdGhleeKAmXJlIG91dC1vZi10aGlzLXdvcmxkIGRlbGljaW91cyEKClBsdXMsIHRoZXJl
4oCZcyBubyBmdW5ueSBpbmdyZWRpZW50cywgbm8gY2hpbGxpbmcsIGV0Yy4gSnVzdCBhIHNpbXBs
ZSwgc3RyYWlnaHRmb3J3YXJkLCBhbWF6aW5nbHkgZGVsaWNpb3VzLCBkb3VnaHkgeWV0IHN0aWxs

Reader Interactions

Comments Cancel reply

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

Primary Sidebar

Tools

Secondary Sidebar

  • Terms of Use
  • Disclaimer
  • Privacy Policy
Manage your privacy
We use technologies like cookies to store and/or access device information. We do this to improve browsing experience and to show (non-) personalized ads. 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.
  • 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.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}