# 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
Comments