Problems with trim()

Welcome Forums General PowerShell Q&A Problems with trim()

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 week, 1 day ago.

  • Author
    Posts
  • #176641

    Participant
    Topics: 3
    Replies: 5
    Points: 29
    Rank: Member

    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

  • #176647

    Participant
    Topics: 1
    Replies: 19
    Points: 54
    Rank: Member

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

    Example:

    
    PS C:\> $test = "user1@example.com              "
    PS C:\> $test.Length
    31
    PS C:\> $test = $test.Trim()
    PS C:\> $test.Length
    17
    
    
  • #176650

    Participant
    Topics: 1
    Replies: 1529
    Points: 2,587
    Helping Hand
    Rank: Community Hero

    I think it does not work as you expect it to. 😉 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.

  • #176653

    Participant
    Topics: 3
    Replies: 5
    Points: 29
    Rank: Member

    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

  • #176659

    Participant
    Topics: 3
    Replies: 5
    Points: 29
    Rank: Member

    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

You must be logged in to reply to this topic.