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.
$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:
\\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:
\\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:
$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.
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
$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.
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…