Powershell Split String

#Powershell

I wanted to put a disclaimer in my profile.ps1 file, but I wanted it to look nice.  I wanted it to split at 67 characters and split whole words.

It was a painful exercise, but this works!

$message="This system is the property of Flaphead.com, and provided only for authorised used according to Flaphead.com policies. This system may be subject to monitoring for lawful purposes and to ensure compliance with Flaphead.com policies in accordance with all applicable legislation. Use of this system constitutes consent to lawful monitoring, Flaphead.com policies and all applicable legislation."

$SplitAt = 67
$Start = 0
$MessageLength = $Message.Length
$MessageArray = @()
$more = $True
While($more){
$CharsLeft = $MessageLength - $Start
$splitNow = [math]::min($SplitAt, $CharsLeft)
$chars = $Message.substring($start,$SplitNow)
if($splitNow -ne $SplitAt){$tmpLine = $Chars}ELSE{ $tmpLine = $Message.substring($start,$chars.lastindexof(" ")+1)}
$start += $tmpLine.Length
IF([string]::IsNullOrEmpty($tmpLine)){$more=$False}ELSE{ $MessageArray += $tmpLine}
$tmpLine
if($start -gt $MessageLength){$more=$False}
}

Leave a Reply

%d bloggers like this: