process multiple property in same time simplified syntax
PS C:\> $a = @{
>> test1 = 10
>> test2 = 20
>> test3 = 30
>> test4 = 40
>> }
fo example for assign new value to properties types instead of
$a | % {
$_.test1 = 100
$_.test2 = 100
$_.test3 = 100
$_.test3 = 100
}
we use wildcard
for all properties
$a | % {
$_.test* = 100
}
or for some property $_.test1 and $_.test2
$a | % {
$_.test*[12] = 100
}
its powerful
This would break parsing of the *
operator which currently handles mathematical multiplication operations:
$a.test = 100
$a.test*= 100
$a.test # result: 10000
Equally, since *
is a valid property and hashtable key name if enclosed in quotes, this would be a breaking change here also:
$a."test*" = 100
$a # will show the key "test*" with a value of 100
You can currently retrieve properties with a wildcard: [pscustomobject]$a | ForEach-Object -MemberName test*
but I'm not sure there's a way to set them.
Good points, @vexx32.
As for _setting_ wildcard-matched properties:
$o = [pscustomobject] @{ test1 = 10; test2 = 20; test3 = 30 ; test4 = 40 }
$o.psobject.Properties.Name -like 'test*' | % { $o.$_ = 100 }
There's also some built in syntax for it (sorta) with the "magic" methods:
$o = [pscustomobject] @{ test1 = 10; test2 = 20; test3 = 30 ; test4 = 40 }
$o.psobject.Properties.Match('test*').ForEach('Value', 100)
$o
# test1 test2 test3 test4
# ----- ----- ----- -----
# 100 100 100 100
That's handy, @SeeminglyScience.
I assume you're calling it "magic" because the actual collection type returned by .psobject.Properties
doesn't have this method.
Out of curiosity: is there any way to systematically discover all this "magic" throughout PowerShell?
It would be great if the Get-Member
enhancement discussed in #11798 would cover them all.
@mklement0 the .PSObject.Properties
member does have the .Match()
method definition, it's defined by the . (thanks @mklement0 for the correction; it is in fact ReadOnlyPSMemberInfoCollection
type, which is one of the types that that particular collection inheritsPSMemberInfo<T>
that implements this member.)
So that won't really be covered there, but you can discover it the same way you would other things of that nature:
$a = @{}
Get-Member -InputObject $a.PSObject.Properties
Thanks, @vexx32 - I got myself confused too:
While ReadOnlyPSMemberInfoCollection`1
too has a Match
method, it isn't the type providing the Match
method on the type returned from .psobject.Properties
(and psobject.Members
) - instead, it is the type returned _from calling Match
_ on the latter.
The type providing Match
for .psobject.Properties
is System.Management.Automation.PSMemberInfoCollection`1
, which the internal System.Management.Automation.PSMemberInfoIntegratingCollection`1
type that .psobject.Properties
returns an instance of inherits from.
That said, you're right in essence: Get-Member
is sufficient to discover the method.
PSMemberInfoCollection<>
is the base type that defines Match
. By magic I meant that the ForEach
magic method can be used to set a value.
Thanks for clarifying, @SeeminglyScience (the word Collection
was missing from the type name in my previous comment (since corrected) - System.Management.Automation.PSMemberInfoCollection`1
is indeed what I meant).
As for the word "magic", to close the tangent: let me again suggest the terminology "automatic method", following the pattern of "automatic variable" - after all, it's not magic; it is built in, and therefore _automatically available_.
If someone googles automatic method they're not gonna find anything. Get it official and documented and I'll switch 馃檪
I'll save any naming critiques for where ever that issue is
Yes, having _established, agreed-upon terminology_ is very important - not just for discoverability, but also for rasing awareness of the underlying _concepts_.
_Establishing_ terminology is hard, not least because not everyone is focused on the importance of doing so.
https://github.com/PowerShell/PowerShell/issues/11798#issuecomment-583426062 first proposed the term _automatic member_, and it seemed like an apt place to start the discussion, given the context of suggesting that such members for the first time be made _programmatically discoverable_.
But you're right: let us tackle this directly: please see https://github.com/MicrosoftDocs/PowerShell-Docs/issues/5627
Related: Enhance hash table syntax #13817: 1..4 | New-Dictionary -KeyScriptBlock { "Test$_" } -ValueScriptBlock { 10 * $_ }
Most helpful comment
There's also some built in syntax for it (sorta) with the "magic" methods: