Functions/Split-Line.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
Function Split-Line { <# .SYNOPSIS To split a string line by line and return a string array .DESCRIPTION To split a string line by line and force it to return a string array. Here strings, script EOL character sequence, and system default NewLine character can muck up parsing a string into lines. This is an attempt to address this. .PARAMETER String The string that you want split line by line .EXAMPLE $HereString = @' Server1 Server2 Server3 '@ $Object = Split-Line -String $HereString $Object.Count 3 .EXAMPLE (Split-Line -String 'SimpleString').GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array #> [CmdletBinding(ConfirmImpact='None')] [OutputType('string[]')] param( [Parameter(Mandatory, HelpMessage='Enter a string composed of tokens',Position=0,ValueFromPipeline)] [string] $String ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" } process { if ($String -notmatch "`n") { Write-Output -InputObject (,([array] $String)) } else { $ReturnValue = $String -split "`r`n" if ($ReturnValue.Count -eq 1) { $ReturnValue = $String -split "`n" } Write-Output -InputObject $ReturnValue } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |