String methods and arrays

In PowerShell, some string methods can be called on an array. The method will be executed against each of the elements in the array. For example, the trim method is used against each of the strings:

('azzz', 'bzzz', 'czzz').Trim('z') 

The split method is also capable of acting against an array:

('a,b', 'c,d').Split(',') 

This remains true as long as the array object does not have a conflicting method or property. For example, the Insert method cannot be used as an array object has a version of its own.

Properties and methods of array elements:
The feature demonstrated here has broader scope than methods, and it applies to more than string objects.
In the case of strings, the methods that can be used can be viewed as follows:
$arrayMembers = (Get-Member -InputObject @() -MemberType Property, Method).Name
'string' | Get-Member -MemberType Property, Method |
Where-Object { $_.Name -notin $arrayMembers }
Using this feature with DateTime objects, the AddDays method may be called on each element in an array:
((Get-Date '01/01/2017'), (Get-Date '01/02/2017')).AddDays(5)
Or the DayOfWeek property may be accessed on each element in the array:
((Get-Date '01/01/2017'), (Get-Date '01/02/2017')).DayOfWeek
A similar Get-Member command reveals the list of properties and methods that may be used in this manner:
Get-Date | Get-Member -MemberType Property, Method |
Where-Object { $_.Name -notin $arrayMembers }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.167.195