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



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.



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.

$b=$a.trim() -replace '\s+',' ' -replace '>','' -split ' '


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]}


Fine. Which brings me to the TrimStart method.


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



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?



Now we will remove the slash at the beginning …



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


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



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

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

    result: patrick@outlook

    eg: x=2

    result: patrick@outlook.c

    eg: x=default

    result: patrick@outlook.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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