20

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.

1

2 Answers 2

31

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 ","
Sign up to request clarification or add additional context in comments.

1 Comment

Even Shorter - you can also simply treat the variable as a single object and just specify a property name. i.e. PowerShell will automatically expand dir.name into the same thing as dir | Select -ExpandProperty name.
10

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

(dir -name) -join ','

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.