![](/style/images/good.png)
![](/style/images/bad.png)
Calculated Properties in PowerShell
source link: https://thomasrayner.ca/calculated-properties-in-powershell/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Calculated Properties in PowerShell
Most of the time, a PowerShell cmdlet will return all the information you need to work with it later in the pipeline. Sometimes, though, there’s some assembly required. What I mean, is maybe the cmdlet returned the information you need, but not in the format you want, or you wish you had some property multiplied by some other property. Let’s explore.
Say you ran Get-ChildItem to get some items in a directory, and you get something like the following.
PS> Get-ChildItem c:\temp\demo
Directory: C:\temp\demo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 6/5/2017 8:40 AM 1519200 thing.txt
One of the items is Length, which tells you the size of the file in bytes. What if I wanted that in kilobytes, though? Well, it’s not too hard. We’re going to use a calculated property, using Select-Object.
PS> Get-ChildItem c:\temp\demo | Select-Object -Property Name, @{label = 'FileSize'; expression = { $_.Length/1KB }}
Name FileSize
---- --------
thing.txt 1483.59375
So I’m selecting two properties. One is Name, and the other is a calculated property. A calculated property is basically a hashtable with two items in it: a label, which is the name of our calculated property, and expression, which is the scriptblock that defines our calculation.
In this case, the name of my calculated property is FileSize, and the calculation is “the length of the item, divided by 1KB”. In these calculations, $_ basically refers to “the item we’re looking at”.
Recommend
-
36
Recently I was working on an MDX calculation that calculated a value at a low granularity and aggregated the result – basically what I described in myblog post here. Here’s a simplified version of the calculation written...
-
42
By:Eli Leiba | Last Updated: 2018-11-23 | | Related Tips:More > T-SQL Problem I need to perform matri...
-
10
PowerShell - Control What Order Properties Are Displayed On A Custom Objects And Hash Tables There are a handful of different ways to create custom objects in PowerShell, including building one from a hash table. You mig...
-
8
Beginner PowerShell Tip - Using Variable Properties In Strings If you’re just getting started in PowerShell, it’s possible that you haven’t bumped into this specific issue yet. Say you’ve got a variable named $user
-
4
Calculated story telling sets heightened expectations I think I better understand why some people say that TV rots your brain. I'm not talking about whether a flickering box of light or fast-paced cuts or any of that stuff mesmer...
-
12
ConversationI actually think someone recently (accidentally?) moved the output function a couple of lines lower in the code, to just below the human key press. In my scripting language, CTRL+T causes the line you're on and the...
-
8
Rails: calculated column caching Posted: 2007-12-06 - Last updated: 2019-06-05 Tagged
-
8
PowerBI & Big Data – Using pre-calculated Aggregations of Semi- and Non-Additive Measures Calculating and visualizing semi- and non-additive measures like distinct count in Power BI is usually not a big deal. H...
-
8
Powershell script for finding properties for a limited number of users on the AD advertisements I am trying to run a PS script of a limited nu...
-
3
How To: Use Azure AD Powershell to Work With Extension Properties (User Attributes) 1 minute read |
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK