3

The 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.

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.