-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select-Object hashtable syntax is long-winded, support a shorter version? #8107
Comments
I would actually recommend also supporting collapsing of the syntax into a single hashtable as well. Your last example could look like this instead: gci | select @{ShortName='BaseName'; Size={$_.Length / 1MB}}
# or
Get-ChildItem | Select-Object -Property @{
ShortName = 'BaseName'
Size = { $_.Length / 1MB }
} |
Wouldn't it be just as wise to change the Expression from a statically Calculated property, To a more fluid property such as a ScriptProperty.
|
If that is done then there becomes no way to actually use a static calculated property when you might want that, though. And I think in most cases a static noteproperty is all most folks want. I'd think it would be more effective to implement that as a separate |
Using Add-Member seems to be a step into the past. Using a calculated field is a simpler operation. simplifying the syntax of calculated fields (this affects format-table and sort-object as well) is a good idea as long as the original syntax is still available otherwise you're going to break a ton of code. |
I'd also want to retain the ability to mix the properties from the object with the calculated fields so something like gci | select Mode, @{ShortName='BaseName'}, LastwriteTime, @{Size={$_.Length / 1MB}}, CreationTime |
This is useful for Format-* cmdlets too. Can/should we this in the same time? |
I implemented this feature as a proof-of-concept for myself a few months ago, but haven't polished it or added tests. (Happy to contribute the changes, but they're definitely not PR-ready.) Observations:
Complications:
Related, helpful potential changes:
|
It seems that propertly supporting the Format* cmdlets and having it collapse down to one hashtable with multiple properties makes it much harder. Those are things I don't really want, as much as the change to Select-Object. Would it be possible to handle select-object in one change and those later?
It would be a breaking change if |
Yes, of cause. I meant that there may be a common code. |
Special-casing Select-Object would require that it abandons the common code that all the hashtable-parameter-using cmdlets share. Plus, the collapsed-hashtable-argument style is great for the other cmdlets too - but there are some corner cases.
Requiring [ordered] to get proper results when using the short-form syntax will cause people to make mistakes, forever. KeyValuePair literals are probably the only clean-ish solution to this problem; all the other opitons involve introducing some quirky behavior. |
@HumanEquivalentUnit, thanks for linking my issue. Taking @bstrautin example: Implicit syntax: gci | ft2 @{
SizeInBytes = {$_.Length}, '#,0', 'right'
SecondsOld = {([datetime]::UtcNow-$_.LastWriteTimeUtc).TotalSeconds}, '#,0', 'right'
LastModified = {$_.LastWriteTime}, 'yyyy-mm-dd'
} Explicit syntax: gci | ft2 @{
SizeInBytes = [Expression]'Length', [FormatString]'#,0', [Alignment]'right'
SecondsOld = [Expression]{([datetime]::UtcNow-$_.LastWriteTimeUtc).TotalSeconds}, [FormatString]'#,0', [Alignment]'right'
LastModified = [Expression]'LastWriteTime', [FormatString]'yyyy-mm-dd'
} The disadvantage is that the concerned property attribute classes need to be predefined. At the other hand, this could also be turned into an advantage if the properties attributes could be attached to the concerned object properties to be used as a default behavior for the related cmdlet as I am not sure what I should do with my request #11866, close it? |
@iRon7 if your request is effectively a duplicate, probably best to close it and summarise any extra points you feel aren't covered in this issue already (a bit like you already did, but feel free to add on any extra points that haven't been covered yet) 🙂 |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
2 similar comments
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has an active linkage at mentioned this issue on Oct 2 |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
Select-Object's hashtable syntax - even with abbreviating the key names to one character, it's a lot of code and symbols, and feels like it could be cleaner. The simple case of taking one property value unchanged, but with a new name, could be a lot cleaner.
What if a single-key hashtable could be used, where the key is the new property name, and the value is either a string of the original property to rename, or a scriptblock calculation. e.g.
The text was updated successfully, but these errors were encountered: