In PowerShell it’s easy to manipulate strings, especially to cut off some characters from a string. These three methods are your friends when it comes to trimming: Trim, TrimStart and TrimEnd. In this blog post I will show you how to use PowerShell to manipulate strings and cut off something at the beginning and at the end. I will also try to give you some useful practical examples in hand to convince you that it’s worth to learn something about string manipulation. Let’s hop in.
Trim
Trim can serve you with
- removing all white-space characters
- removing a set of leading characters
Let’s say we have a string like ‘ Patrick Gruenauer ‘. If you want to remove all white-space characters then you put trim at the end of the sring with no arguments.
' Patrick Gruenauer '.Trim()

How do you know that worked? Let’s compare the character length.
' Patrick Gruenauer '.Length ' Patrick Gruenauer '.Trim().Length

Now let’s remove a set of leading or ending characters.
'Patrick Gruenauer'.Trim("er")

'Patrick Gruenauer'.Trim('Pa')

Fine.
What is this all for?
For instance you want to show all logged on users. Quser.exe shows you all logged on users on the local computer. The quser output presents you with an ugly text-based output. Now let’s say you want to display only the username.
quser

To display only the username you need to use PowerShell methods to manipulate the string as desired. One of the methods is Trim to remove all white-space characters.
$a=quser $b=$a.trim() -replace '\s+',' ' -replace '>','' -split ' ' $b[8]

Another example is to show the ARP cache, but you want to show only the IP-Address without any other text.
Here is the original view.

And here is the manipulated view for showing only IP-Addresses. Trim will remove all leading and trailing white-space characters.
$a=arp -a | Select -Skip 3 $b=$a.Trim() -replace '\s+',' ' -split '\s' $c=for ($i=0;$i -lt $b.count;$i+=3) {$b[$i]} $c

Fine. Which brings me to the TrimStart method.
TrimStart
TrimStart removes characters at the beginning. Here is an example.
$a='patrick@outlook.com' $a.TrimStart('pa')

And here is an another example of removing a set of leading characters. $env:logonserver shows you your logonserver which is usually a Windows Server Domain-Controller, right?
$env:logonserver

Now we will remove the slash at the beginning …
$env:logonserver.Trim('\')

… in order to ping your domain-controller … the computername is retrieved from the logonserver environment variable …
Test-Connection -ComputerName $env:LOGONSERVER.TrimStart('\') -Count 1

Cool stuff.
TrimEnd
TrimStart removes characters at the end. Here is an example.
$a='patrick@outlook.com' $a.TrimEnd('.com')

Windows operating systems displays the IPv6 adresses with the netword card id the end of the IPv6 address. If you want to remove the network card id, use TrimEnd.

(Get-NetIPAddress -AddressFamily IPv6 -InterfaceIndex 12 | Select-Object -ExpandProperty IPAddress).TrimEnd('%12')

Great. That’s it for today with string manipulation.
See also
PowerShell: Playing with text or how to get every x line from text output (trim,replace,split)
Categories: PowerShell
how to trim end of string by “user defined” number of characters?
can a default option (eg 3) be set if user just hits and continues?
not sure exactly, but something like:
$string1 = $string.TrimEnd(x)
eg: x=4
$a=’patrick@outlook.com’
$a.TrimEnd(x)
result: patrick@outlook
eg: x=2
$a=’patrick@outlook.com’
$a.TrimEnd(x)
result: patrick@outlook.c
eg: x=default
$a=’patrick@outlook.com’
$a.TrimEnd(x)
result: patrick@outlook.
LikeLike
Very useful to me
Thanks a lot
LikeLike
Thank you!
LikeLike
nice
LikeLiked by 2 people
Great
LikeLiked by 1 person
Thank you
LikeLike