Working with text


Split line example

$mytext = "line1
line2
line3
Line4"
$lines = $mytext.split("`n").Trim()

#loop in each line

foreach ($line in $lines)
{
write-host "This is $line"
}

Replace tab with space

$mytest = "Hi tab1	how are you	tab2	? bye	tab3	bye"
$mytest
$mytest.replace("`t", " ")

#below is the result
Hi tab1 how are you tab2 ? bye tab3 bye

Spilt file or directory path

# remove the last path
$mypath = "C:\program\test\myfile"
Split-Path $mypath
#result
C:\program\test

# show only last path
$mypath = "C:\program\test\myfile"
Split-Path $mypath -Leaf
# result
myfile

Loop each work in the text

$mytext = "Hi tab1 how are you tab2 ? bye tab3 bye"

foreach ($line in $mytext.Split()) {$line}

# Result
Hi
tab1
how
are
you
tab2
?
bye
tab3
bye