powershell output to one line

I am running some powershell one liners (with vmware powercli)  to look at data, here is what I am running
Get-VM | where-object {$_.powerstate -eq "poweredon"} |Get-Annotation -CustomAttribute 'location','Host'

Open in new window

The output looks like this, and I also tried it in out-gridview, and export-csv all are like this.
AnnotatedEntity                    Name     Value
---------------                               ----          -----
server1                                   location  datacenterE
server1                                   host        host1
server2                                   location  datacenterW
server2                                   host        host3

I would like to look like this:
AnnotatedEntity                    Name     Value        Name            value
---------------                               ----           -----           -------              ------
server1                                    host        host1       location         datacenterE
server2                                    host        host3       location         datacenterW

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:

Get-VM | where-object {$_.powerstate -eq "poweredon"} |Get-Annotation -CustomAttribute 'location','Host'| Group-Object AnnotatedEntity

Open in new window

You should get something similar, try it and report to check how we can fix it :)
You can turn the attribute names into properties:
Get-VM | Where-Object {$_.powerstate -eq "poweredon"} | Get-Annotation -CustomAttribute 'location', 'Host' |
	Group-Object -Property AnnotatedEntity |
	ForEach-Object {
			'Location'=($_.Group | ? {$_.Name -eq 'Location'}).Value;
			'Host'=($_.Group | ? {$_.Name -eq 'Host'}).Value

Open in new window

Or as a one-liner:
Get-VM | Where-Object {$_.powerstate -eq "poweredon"} | Get-Annotation -CustomAttribute 'location', 'Host' | Group-Object -Property AnnotatedEntity | ForEach-Object {[pscustomobject]([ordered]@{AnnotatedEntity=$_.Name; 'Location'=($_.Group | ? {$_.Name -eq 'Location'}).Value; 'Host'=($_.Group | ? {$_.Name -eq 'Host'}).Value})}

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
For the fun of it, here's a script that will do everything automatically, even if you query for different attributes. Save it, for example, as C:\PS\ConvertFrom-Annotation.ps1 or wherever, then pipe the results from Get-Annotation to it:
Get-VM | Where-Object {$_.powerstate -eq "poweredon"} | Get-Annotation -CustomAttribute 'location', 'Host' | C:\PS\ConvertFrom-Annotation.ps1

Open in new window

	[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
Begin {
	$AnnotationList = @()
Process {
	$InputObject | ForEach-Object {$AnnotationList += $_}
End {
	$CustomAttributes = $AnnotationList | Select-Object -ExpandProperty Name -Unique
	$AnnotationList | Group-Object -Property 'AnnotatedEntity' | ForEach-Object {
		$Result = '' | Select-Object -Property (@('AnnotatedEntity') + $CustomAttributes)
		$Result.AnnotatedEntity = $_.Name
		ForEach ($Attribute In $CustomAttributes) {
			$Result.$Attribute = ($_.Group | Where-Object {$_.Name -eq $Attribute}).Value

Open in new window

SquigglyMonkeyAuthor Commented:
Thank you! Perfect
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.