Easy way to split strings with PowerShell

I was asked about an easy way to split strings with PowerShell. The way that would allow to define the amount of characters of the newly generated strings. After a while I have prepared the following filter:

filter Split-String{ 
        Param([int]$CharCount = 2)
        PROCESS{ $_ -split "(?<=\G.{$CharCount})" }
}

The outcome is as follows:

C:\> “whatever” | Split-String -CharCount 3
wha
tev
er

Leave a Reply

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