Question About Data Returned from a SQL Query

I am returning rows from an invoke-sqlcmd cmdlet. From a previous question I had, I was provided the following for 'extracting' the data into a file:
$accounts | % {"{0, 26}" -f $_[0]} | Add-Content -Path $outTextFile
When I use that now, my output is right-justified. I want it left-justified and trailing spaces trimmed. I suppose some day I am going to have to sit down and learn everything there is about PowerShell :-), but until that day, I rely on Google first and EE second. This one I'm not able to find any suggestions on.
$accounts contains one column named [Data]

Also, can someone explain to me exactly what % {"{0, 26}" -f $_[0]} is doing? Like I said, I asked a question, got this answer and it worked for that problem. I should have asked at that time, but didn't.
LVL 16
Doug BishopDatabase DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Doug BishopDatabase DeveloperAuthor Commented:
Okay, found part of it. Changed 50 to -50 and now it is left-aligned.
How about trimming?
David Johnson, CD, MVPOwnerCommented:
is the string "ABCDE" or "ABCDE          "  ?
"{0, 26}  = variable 0, minimum width 26 right justified
Doug BishopDatabase DeveloperAuthor Commented:
David,

Does it matter (not trying to be snotty, really want to know)?
If there is trailing space I want it trimmed.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

David Johnson, CD, MVPOwnerCommented:
if you don't want spaces why are using a fixed minimum length for the output?

if you have $variable then $variable.trim() will remove leading/trailing spaces.
% {"{0, 26}" -f $_[0]}
%  expands to foreach-object
"{0,26}" -f  format variable $_[0] right justified minimum 26 characters
$_[0]   first element of an object

My opinion is that making the code as compact as possible reduces readability and maintainability. which you you rather maintain
import-csv -file abc.csv |  % {"{0, 26}" -f $_[0]} {
# do something here
}

Open in new window

or
$data = import-csv -file abc.csv | select name,street,city,state,country
#$data.name = "{0, 26}" -f $data.name #untested and don't know why
$data.name.trim()
foreach ($name in $data.name) {
     # do something here
     }

Open in new window

Joshua GrantomSenior Systems AdministratorCommented:
You could have it trim the end of each line before adding to the content

$file = $accounts | % {"{0, 26}" -f $_[0]} 
$file | % {$_.TrimEnd()} | Add-Content $outTextFile

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Doug BishopDatabase DeveloperAuthor Commented:
The {0, 26) was a carryover from another project. The data is variable length. If using {0, 26} "forces" it to be written as fixed-length, then what to I need to do to write it out as variable length, and not worry about having to trim the data?

It appears that all I really need is:
$file | % {$_.TrimEnd()} | Add-Content $outTextFile

I will give that a try and see what happens.
Doug BishopDatabase DeveloperAuthor Commented:
The following worked. No need to use TrimEnd().

$file | % {"{0}" -f $_.Data} | Add-Content -Path $outTextFile

Not sure how I'll divvy up points. I'll work on it tomorrow.
Vitor MontalvãoMSSQL Senior EngineerCommented:
dbbishop, do you still need help with this question?
Doug BishopDatabase DeveloperAuthor Commented:
Sorry for the delay. I had a vacation thrown in last week :-). I think Joshua's post got me on the right track, so I am accepting his solution, even though I ended up tweaking it some.
Thanks for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.