Powershell Property Rename

When using Powershell, we sometimes have to change the names of the properties returned by a cmdlet. There are many reasons for doing this, take exporting data as an example. You want to export an array of objects and set custom property names in order to format it as requested.

Another important reason for changing the name of a property it passing objects down the pipeline. When using the pipeline, the property may be named differently that the parameter name for the next command. You'll run into this kind of problems when using cmdlets from differents modules like ActiveDirectory and Exchange. A property may be named "ComputerName" for an AD cmdlet and "Server" for an Echange cmdlet.

Let's see what you can do about it...

For example, we want to test network connectivity on port 389 towards all domain controllers. First we will get the list of the domain controllers using the cmdlet Get-ADDomainController and then we will test connectivity using Test-NetConnection.

As you'll see the Get-ADDomainController returns the server's name as "HostName":



Using the @{name="ComputerName";Expression={$_.hostname}} we set the property name to "ComputerName" and the value to be the value of "HostName"


Now we can pipe this to Test-NetConnection:


 

Popular posts from this blog

Domain Controller Machine Password Reset

Managing Active Directory User Certificates using PowerShell

Configuring a Certificate on Exchange Receive Connector