FredTheWebGuy
FredTheWebGuy

Reputation: 2586

Alias a Property Name In Powershell 3

Using the example below:

Get-Service | ConvertTo-HTML -Property Name, Status > C:\services.htm

I was wondering if it is possible to alias the property name - the same way you could in SQL:

Example:

Get-Service | ConvertTo-HTML -Property Name AS NEWNAME , Status AS MYNEWSTATUSNAME> C:\services.htm

I know the above syntax wouldn't work... What is the correct way to alias a property name?

Upvotes: 26

Views: 50440

Answers (3)

Mike Shepard
Mike Shepard

Reputation: 18156

How about using select-object?

get-service | select-object -property @{N='MyNewStatus';E={$_.Status}}, @{N='MyNewName';E={$_.Name}} | ConvertTo-HTML > C:\services.htm

Upvotes: 44

mjolinor
mjolinor

Reputation: 68273

The way to alias a property name is to add an AliasPropery to the object.

Get-Service | 
foreach {
$_ | Add-Member -MemberType AliasProperty -Name MYNEWSTATUSNAME -Value Status -PassThru
} |
Select Name,MYNEWSTATUSNAME

Upvotes: 19

zdan
zdan

Reputation: 29450

You could do an intermediate step of creating objects with the property names you want using the new-object cmdlet.

Get-Service | foreach{ new-object PSObject -property @{newname=($_.Name); newstatus=($_.Status)}} | ConvertTo-Html > .\services.htm

Upvotes: 4

Related Questions