Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias a Property Name In Powershell 3

Tags:

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?

like image 850
FredTheWebGuy Avatar asked Apr 23 '13 17:04

FredTheWebGuy


People also ask

What is an alias property in PowerShell?

An alias is an alternate name or nickname for a cmdlet or for a command element, such as a function, script, file, or executable file. You can use the alias instead of the command name in any PowerShell commands.

How do I set an alias in PowerShell?

PowerShell includes built-in aliases that are available in each PowerShell session. The Get-Alias cmdlet displays the aliases available in a PowerShell session. To create an alias, use the cmdlets Set-Alias or New-Alias . In PowerShell 6, to delete an alias, use the Remove-Alias cmdlet.

How do I add an alias to my PowerShell profile?

To create an alias for a command, create a function that includes the command, and then create an alias to the function. To save the aliases from a session and use them in a different session, add the Set-Alias * command to your Windows PowerShell profile. Profiles do not exist by default.


2 Answers

How about using select-object?

get-service | select-object -property @{N='MyNewStatus';E={$_.Status}}, @{N='MyNewName';E={$_.Name}} | ConvertTo-HTML > C:\services.htm 
like image 158
Mike Shepard Avatar answered Sep 21 '22 06:09

Mike Shepard


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 
like image 36
mjolinor Avatar answered Sep 19 '22 06:09

mjolinor