I often paste input into ISE and sometimes it needs trimming empty lines and leading/trailing spaces, so I need to pipe it to trim those:
$lines = $null
$lines = @"
\\server\share\folder
\\server\share\folder2
\\server2\share\folder
\\server2\share\folder2
"@ -split "\t|\r?\n" |?{$_ -match "\w"} |ForEach-Object {($_ -replace "").trim()}
So, using
$_ -match \w covers me when the lines contain letters and/or numbers, which works in my particular case, but I assume there is a proper way to exclude empty lines?
Also,
($_).trim() doesn't seem to work, so I am having to do something to
$_ , like
$_ -replace "" (which I am hoping does nothing?), before trim actually works. So, what is the proper way to trim the object in the pipeline? I am guessing turn it into a string somehow?
Or is there a better way of doing all this altogether?
My goal is basically paste some input into ISE, split it into a multi line object, remove the empty lines, and trim the remaining lines.
Thank you!
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.