Here is something that I have used for a while but had no idea what it was called. After a bit of googling, apparently its a calculated property.
What is a calculated property?
A calculated property is changing the name and/or value of an attribute as its copied to a new object or the screen.
A quick example is shown below.
$result = $object | select att1, att2, @{Name="NewAtt";Expression={$_.att3 + 1}}, att4
Here I am copying $Object and its attributes att1, att2 and att4 to $Result. I’m also copying att3 but I’m renaming it to NewAtt and adding 1 to its value.
Basically the calculated property is a hash table. The fist key is the Name or Label and the second key is the Expression.
If you feel lazy you can shorten the hash table to the following. I tend not to do this as I think it make the code harder to read.
$result = $object | select att1, att2, @{N="NewAtt";E={$_.att3 + 1}}, att4
Using a calculated property
Starting with a simple get-childitem command.
I now pipe Get-ChildItem in to a select command to select what attributes I need.
I think Length is a silly name for filesize. I want to use Size instead. So I use.
Get-ChildItem | select name, mode, @{Name="SizeBytes";Expression={$_.length}}, lastwritetime
But now I dont want “Size” in bytes, I want “Size” in kb. So I use.
Get-ChildItem | select name, mode, @{Name="SizeK";Expression={$_.length / 1kb}}, lastwritetime
If you have not used calculated propertys before then you may think “why would I ever need this?” well if you are collecting data then you will need this sooner or later.
I needed it sooner.
anyhow I hope I have save people some searching.