0

I think I'm missing something simple. I need to append text to each line of output from a Get-ChildItem instruction.

As a simplistic example, picture adding a reference number ('12345') to a folder listing on c:

The output I'm after would look something vaguely like:

c:\Program Files 12345
c:\program Files (x86) 12345
c:\temp 12345
c:\Users 12345
c:\Windows 12345
...etc

The real application has more to it, but the end result is that a Powershell script runs a bunch of these commands with some metadata appended to each line which then gets pulled into Excel for reporting.

I just can't figure how to add the string to each line...

  • Excel sounds like CSV. If so do it the proper way and use Export-Csv and give it a data structure to export. Otherwise what did you try to make it happen? A simple string concatenation would work as well or just using an output function with the right quotation marks and argument. – Seth Jan 10 '19 at 6:27
  • I said it was probably simple - string concatenation would almost certainly do the trick but I'm missing how to do that for every line of output – PerryW Jan 10 '19 at 6:31
  • This might give you a start gci c:\ -Directory -Recurse | select fullname, {'12345'} – Lieven Keersmaekers Jan 10 '19 at 7:32
  • Or if you prefer strings (you shouldn't) perhaps this gci c:\ -Directory -Recurse | select {"$($_.fullname) 12345"} – Lieven Keersmaekers Jan 10 '19 at 7:34
  • A loop would also work. 1..10 | %{ ("ABC $_") }. If you plan to generate a CSV you should use a proper way. – Seth Jan 10 '19 at 9:30
1

To have the numbers trailing directly to the folder names you can:

gci C:\ -Directory | % { $_.FullName + " 12345"}

More complex with a calculated property

(gci c:\ -Directory | select @{n='WithText';e={$_.FullName + " 12345"}}).WithText
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.