简体   繁体   中英

Converting Powershell command result to one line

I am new to powershell and stuck with formatting the results. I have result of my command like below in horizontal format in a row which is of type "object"

Name
----
Test Mrs. User12  
Test Mrs. User13  
Test Mr. User15  
Test Mr. User20

How can I get only values printed in

"Test Mrs. User12;Test Mrs. User13;Test Mr. User15;Test Mr. User20"

Thanks in Advance.

If

My-Command | Select-Object "Name"

Outputs

Name
----
Test Mrs. User12  
Test Mrs. User13  
Test Mr. User15  
Test Mr. User20

You should be able to concatenate them with the -join operator, like so:

(My-Command | Select-Object -ExpandProperty "Name") -join ";"

The -ExpandProperty parameter takes the value of the Name property from each object, and throws everything else away, so you end up with an array of strings.

When you apply the -join operator with the argument ";" , all the strings will be concatenated with a ";" in between each string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM