# Problems with trim()

**URL:** <https://forums.powershell.org/t/problems-with-trim/13136>\
**Category:** PowerShell Help\
**Created:** [September 12, 2019, 7:57pm UTC](https://forums.powershell.org/t/problems-with-trim/13136 "2019-09-12T19:57:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisfdu-edu](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@chrisfdu-edu](https://forums.powershell.org/u/chrisfdu-edu)\
**Post date:** [September 12, 2019, 7:57pm UTC](https://forums.powershell.org/t/problems-with-trim/13136/1 "2019-09-12T19:57:08Z")

</div>

Hi All,

I recently realized I was having issues with a script because of extra white space. I am calling the trim function but it’s not doing anything. Am I calling it wrong?

```
$test = "user1@example.com "
$test.Length
$test.TrimEnd()
$test.Length
$test.Trim()
$test.Length
$test

PS C:\temp> $test = "user1@example.com "
$test.Length
$test.TrimEnd()
$test.Length
$test.Trim()
$test.Length
$test
36
user1@example.com
36
user1@example.com
36
user1@example.com

PS C:\temp>
```

-Chris

---

<div class="post-metadata">

**Author:** ![ilikecheese](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@ilikecheese](https://forums.powershell.org/u/ilikecheese)\
**Post date:** [September 12, 2019, 8:10pm UTC](https://forums.powershell.org/t/problems-with-trim/13136/2 "2019-09-12T20:10:37Z")

</div>

Perhaps I am missing something, but you are not putting the new value that has been trimmed, back into the “test” variable.

Example:

[pre]

PS C:&gt; $test = "[user1@example.com](mailto:user1@example.com) "  
PS C:&gt; $test.Length  
31  
PS C:&gt; $test = $test.Trim()  
PS C:&gt; $test.Length  
17

[/pre]

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 12, 2019, 8:15pm UTC](https://forums.powershell.org/t/problems-with-trim/13136/3 "2019-09-12T20:15:58Z")

</div>

I think it does not work as you expect it to. 😉 Let’s take your first 4 lines of code …

```
$test = "[user1@example.com](mailto:user1@example.com)"
$test.Length
$test = $test.TrimEnd()
$test.Length
```
  
If you want to make your change permanent to the variable you have to assign it.

---

<div class="post-metadata">

**Author:** ![chrisfdu-edu](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@chrisfdu-edu](https://forums.powershell.org/u/chrisfdu-edu)\
**Post date:** [September 12, 2019, 8:31pm UTC](https://forums.powershell.org/t/problems-with-trim/13136/4 "2019-09-12T20:31:38Z")

</div>

Thanks for the quick responses.

I like cheese - I thought I was performing an operation on the value of the variable

Olaf - Assigning it worked.

I am actually using the trim in a ForEach loop, is it possible to do something like this

Get-ADUser -Filter {userPrincipalName -eq $user.trim() }

or would I need to assign it first?

-Chris

---

<div class="post-metadata">

**Author:** ![chrisfdu-edu](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@chrisfdu-edu](https://forums.powershell.org/u/chrisfdu-edu)\
**Post date:** [September 12, 2019, 8:38pm UTC](https://forums.powershell.org/t/problems-with-trim/13136/5 "2019-09-12T20:38:52Z")

</div>

I Like Cheese & Olaf

Had a chance to think about your responses more and look at it on my computer. Thank you both for helping clear up my understanding of Trim/TrimEnd

-Chris

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/problems-with-trim/13136/6 "2024-05-16T20:31:24Z")

</div>

Closed on May 16, 2024, 8:31pm UTC


