PowerShell

PowerShell: Manipulating Strings with Trim, TrimStart and TrimEnd

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()

1.PNG

How do you know that worked? Let’s compare the character length.


'         Patrick Gruenauer       '.Length
'         Patrick Gruenauer       '.Trim().Length

1.PNG

Now let’s remove a set of leading or ending characters.


'Patrick Gruenauer'.Trim("er")

1.PNG

'Patrick Gruenauer'.Trim('Pa')

1.PNG

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

1.PNG

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]

1.PNG

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.

1.PNG

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

1.PNG

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')

1.PNG

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

1.PNG

Now we will remove the slash at the beginning …


$env:logonserver.Trim('\')

1.PNG

… in order to ping your domain-controller … the computername is retrieved from the logonserver environment variable …


Test-Connection -ComputerName $env:LOGONSERVER.TrimStart('\') -Count 1

1.PNG

Cool stuff.

TrimEnd

TrimStart removes characters at the end. Here is an example.


$a='patrick@outlook.com'
$a.TrimEnd('.com')

1.PNG

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.

1.PNG

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

1.PNG

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

Tagged as: ,

8 replies »

  1. 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.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.