Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Powershell, how do I concatenate one property of an array of objects into a string?

For example, I have 3 files in c:\z

PS C:\z> dir | select name

Name
----
a.png
b.png
c.png

What I want is a string.

a.png,b.png,c.png

Thanks.

like image 341
Just a learner Avatar asked Jul 12 '12 19:07

Just a learner


2 Answers

If you want an array of strings, all you have to do is to:

dir | select -expand name

If you want that as a single string with the values comma separated:

(dir | select -expand name) -join ","
like image 67
manojlds Avatar answered Sep 30 '22 09:09

manojlds


Just a small improvement, you can get names only with the Name switch:

(dir -name) -join ','
like image 39
Shay Levy Avatar answered Sep 30 '22 11:09

Shay Levy