Problems with trim()

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

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:> $test = "user1@example.com "
PS C:> $test.Length
31
PS C:> $test = $test.Trim()
PS C:> $test.Length
17

[/pre]

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

$test = "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.

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

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