# String Method TrimEnd not trimming as expected

**URL:** <https://forums.powershell.org/t/string-method-trimend-not-trimming-as-expected/18770>\
**Category:** PowerShell Help\
**Created:** [March 8, 2022, 7:38pm UTC](https://forums.powershell.org/t/string-method-trimend-not-trimming-as-expected/18770 "2022-03-08T19:38:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [March 8, 2022, 7:38pm UTC](https://forums.powershell.org/t/string-method-trimend-not-trimming-as-expected/18770/1 "2022-03-08T19:38:12Z")

</div>

Hello,

I’m using the String Method `TrimEnd` to trim a specific portion at the end of a string.

I’m importing the string values from a CSV file and looping it through `ForEach-Object`. The string value is a path to my NAS.

```auto
$osd_path = "D:\OSD\Builder"
Import-Csv "$osd_path\List-SourceNames.csv" | ForEach-Object {
$nas_path_root = $($_.nas_path).TrimEnd("\sources")
}
$nas_path_root

```

Output:

```auto
\\nas\Microsoft\Windows 10 Enterprise LTSC 2021
\\nas\Microsoft\Windows 10 21H2 Pro for Workstation
\\nas\Microsoft\Windows 11 21H2 Pro for Workstation
\\nas\Microsoft\Windows 11 21H2 P
\\nas\Microsoft\Windows Server 2022 LTSC Datacenter GUI
\\nas\Microsoft\Windows Server 2022 LTSC Datacenter C

```

All but two of them are incorrect. The four that are being trimmed incorrectly above, should look like this:

```auto
\\nas\Microsoft\Windows 10 21H2 Pro for Workstations
\\nas\Microsoft\Windows 11 21H2 Pro for Workstations
\\nas\Microsoft\Windows 11 21H2 Pro
\\nas\Microsoft\Windows Server 2022 LTSC Datacenter Core

```

However, when I tested this same code against a local directory, they all trim correctly:

```auto
$osd_path = "D:\OSD\Builder"
Import-Csv "$osd_path\List-SourceNames.csv" | ForEach-Object {
$osd_path_root = "$($_.osd_builds)`\$($_.osd_name)`\$($_.osd_sources)".TrimEnd("\sources")
}
$osd_path_root

```

The output here shows only `/sources` was trimmed from the full path; as expected.

```auto
D:\OSD\Builder\OSBuilds\Windows 10 Enterprise LTSC 2021 x64 21H2 19044.1566\OS
D:\OSD\Builder\OSBuilds\Windows 10 Pro for Workstations x64 21H2 19044.1526\OS
D:\OSD\Builder\OSBuilds\Windows 11 Pro for Workstations x64 21H2 22000.493\OS
D:\OSD\Builder\OSBuilds\Windows 11 Pro x64 21H2 22000.493\OS
D:\OSD\Builder\OSBuilds\Windows Server 2022 Datacenter Desktop Experience x64 Dev 22509.1000\OS
D:\OSD\Builder\OSBuilds\Windows Server 2022 Datacenter x64 Dev 22509.1000\OS

```

I even tried using a simple array and looping it with a `Foreach` statement. Both results are exactly the same as the previous outputs: [about\_Foreach](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7.2)

```auto
$osd_path = "D:\OSD\Builder\OSBuilds\Windows 10 Enterprise LTSC 2021 x64 21H2 19044.1566\OS\sources",
"D:\OSD\Builder\OSBuilds\Windows 10 Pro for Workstations x64 21H2 19044.1526\OS\sources",
"D:\OSD\Builder\OSBuilds\Windows 11 Pro for Workstations x64 21H2 22000.493\OS\sources",
"D:\OSD\Builder\OSBuilds\Windows 11 Pro x64 21H2 22000.493\OS\sources",
"D:\OSD\Builder\OSBuilds\Windows Server 2022 Datacenter Desktop Experience x64 Dev 22509.1000\OS\sources",
"D:\OSD\Builder\OSBuilds\Windows Server 2022 Datacenter x64 Dev 22509.1000\OS\sources"

$nas_path = "\\nas\Microsoft\Windows 10 Enterprise LTSC 2021\sources",
"\\nas\Microsoft\Windows 10 21H2 Pro for Workstations\sources",
"\\nas\Microsoft\Windows 11 21H2 Pro for Workstations\sources",
"\\nas\Microsoft\Windows 11 21H2 Pro\sources",
"\\nas\Microsoft\Windows Server 2022 LTSC Datacenter GUI\sources",
"\\nas\Microsoft\Windows Server 2022 LTSC Datacenter Core\sources"

foreach ($os in $osd_path)
{
    $osd_path_root = $osd_path.TrimEnd("\sources")
}
$osd_path_root

foreach ($os in $nas_path)
{
    $nas_path_root = $nas_path.TrimEnd("\sources")
 }
$nas_path_root

```

I was using **Dr Scripto’s** guide on how to [Trim Your Strings with PowerShell](https://devblogs.microsoft.com/scripting/trim-your-strings-with-powershell/).

I’m not really sure why the trimming doesn’t work correctly, and I’m perplexed why there would be an obvious difference between the output from a network path and a local file path.

Any insight or help on this would be greatly appreciated.

Thank you…

---

<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:** [March 8, 2022, 9:20pm UTC](https://forums.powershell.org/t/string-method-trimend-not-trimming-as-expected/18770/2 "2022-03-08T21:20:23Z")

</div>

The code you posted is confusing. So I will use examples to show where your thoughts are flawed.

The method `.TrimEnd()` works EXACTLY as designed but probably different than you think. 😉

The `.TrimEnd()` method removes ANY character independend from its position in the parenthesis from the end of the given string. Here is an example:

```auto
"Windows 10\sources".TrimEnd('screuo\')

```

Instead of the word `\sources` with the backslash in front of it I scrambled the characters but it still removes all of them from the string. I even omitted one “s”. The result is this:

```auto
Windows 10

```

If you have a string with a character next to the part you want to trim equal to ones you want to trim you end up getting more trimmed away than you want. Here’s an example:

```auto
"Windows 10 Pro\sources".TrimEnd('screuo\')

```

Since the “r” and the “o” of “Pro” is provided as the trim characters they will be removed. The result is this:

```auto
Windows 10 P

```

So if you want to remove the last part of a path I’d recommend using the `-replace` operator. Here’s an example:

```auto
"Windows 10 Pro\sources" -replace '\\sources$'

```

---

<div class="post-metadata">

**Author:** ![zero269](https://avatars.discourse-cdn.com/v4/letter/z/da6949/32.png) [@zero269](https://forums.powershell.org/u/zero269)\
**Post date:** [March 9, 2022, 4:02am UTC](https://forums.powershell.org/t/string-method-trimend-not-trimming-as-expected/18770/3 "2022-03-09T04:02:38Z")

</div>

Hello @Olaf,

Thanks for taking a look at this for me. The `TrimEnd` is definitely confusing to me when it comes to removing characters from a given string. The leading and trailing blanks is pretty straight forward, but not the method I was trying to achieve.

I ran your examples, and I got the same results. The best part is that I used the `-replace` option you recommended, and the results worked perfectly for everything.

**Input string:** `\\nas\Microsoft\Windows 11 21H2 Pro\sources`

```auto
foreach ($os in $nas_path)
{
    $nas_path_root = $nas_path -replace '\\sources'
 }
$nas_path_root

```

**Output string:** `\\nas\Microsoft\Windows 11 21H2 Pro`

I still find it strange that two different strings ending in `\sources` can have such different results when trying to actually _trim_ `\sources`. Either way, it’s a battle I hope I never see again.

Thanks again for your help! 👍 👍

---

<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:** [March 9, 2022, 8:27am UTC](https://forums.powershell.org/t/string-method-trimend-not-trimming-as-expected/18770/4 "2022-03-09T08:27:48Z")

</div>

> [@zero269](#):
>
> The `TrimEnd` is definitely confusing to me when it comes to removing characters from a given string.

It’s because you’re using the wrong tool. 😉 `.TrimEnd()` will remove _characters_ - not words. 👆🏽

> [@zero269](#):
>
> I ran your examples, and I got the same results.

But you should be careful when you change code you don’t fully understand. You omitted the dollar sign. It might not change something in this case but it could change the result in other cases.  
Let’s say you have the pattern you want to remove not just at the end of the string.

```auto
'\\nas\sources for OS\Microsoft\Windows 11 21H2 Pro\sources' -replace '\\sources'

```

In this case `-replace` would remove both occurances from the string. While the pattern I suggested only removes the one at the end:

```auto
'\\nas\sources for OS\Microsoft\Windows 11 21H2 Pro\sources' -replace '\\sources$'

```

> [@zero269](#):
>
> I still find it strange that two different strings ending in `\sources` can have such different results when trying to actually _trim_ `\sources` .

Computers are stupid. They will do what you tell them to do. 😉

> [@zero269](#):
>
> Either way, it’s a battle I hope I never see again.

I wouldn’t count on that. 😉 😏

---

<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:18pm UTC](https://forums.powershell.org/t/string-method-trimend-not-trimming-as-expected/18770/5 "2024-05-16T20:18:11Z")

</div>


