Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The following command

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name,VMHost | Where {$_ -match "abc" -or $_ -match "def"} | foreach{$_.Name} | Out-File output.txt

writes a list to output.txt where only the column Name will be printed in the form:

a
b
c
...

Now what I want to achieve is to append ,xxx to each line in some sort of loop, so that I get the following:

a,xxx
b,xxx
c,xxx
...

I tried to append the string but this doesn't seem to work

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name,VMHost | Where {$_ -match "abc" -or $_ -match "def"} | foreach{$_.Name} | Out-File output.txt | Add-Content output.txt ",xxx"

I'm really not familiar with Powershell and I did not find a way to concatenate ,xxx.

In my case it is essential to do the concatenation within a loop, not with a file operation afterwards.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Instead of foreach { $_.Name }, write foreach { "$($_.Name),xxx" }

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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