troubleshooting Question

How to trim current pipeline object in PowerShell?

Avatar of rookie_b
rookie_b asked on
PowershellWindows OS
3 Comments1 Solution12 ViewsLast Modified:
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!


ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros