Questions tagged [member-enumeration]

PowerShell v3+ feature that uses regular dot notation to implicitly retrieve property values from or call methods on all elements of a collection.

Note: Previously, the feature was semi-officially known as just member enumeration, introduced in this 2012 blog post along with the feature itself. A decision to formally introduce the term member-access enumeration was made in early 2022.

For this reason, should be introduced as a synonyn. Please vote for adding it, if you can.

A simple example:

# Get all files in the current dir., as an array.
$files = @(Get-ChildItem -File)

# Use .Name directly on the array to return an enumeration of
# its elements' Name property values.
# It is the faster equivalent of:
#    $files | ForEachObject { $_.Name }
$files.Name
15 questions
187
votes
5 answers

Select the values of one property on all objects of an array in PowerShell

Let's say we have an array of objects $objects. Let's say these objects have a "Name" property. This is what I want to do $results = @() $objects | %{ $results += $_.Name } This works, but can it be done in a better way? If I do something like:…
Sylvain Reverdy
  • 2,468
  • 3
  • 16
  • 14
6
votes
2 answers

How to get a list of a particular field from a list of objects in powershell

I am new to powershell scripting. Apologies if I am missing something simple. Let's say I have an object called object that has a field called field. Now let there be a list of these objects. How would I get the list of fields in the same order? In…
Kaiwen Chen
  • 192
  • 1
  • 1
  • 12
4
votes
3 answers

powershell Get-ChildItem result in array

(Get-ChildItem -File -Recurse -Path $path).Fullname returns array of full names (Get-ChildItem -File -Recurse -Path $path).Name returns array of files names but (Get-ChildItem -File -Recurse -Path $path).Length returns only one value - the…
4
votes
2 answers

Unable to completely parse XML in PowerShell

I have an XML file that I would like to parse through, and retrieve back specific information. To make it easy to understand, here is a screenshot of what the XML file looks like: I would like to parse through the XML and for each Item node,…
Lery
  • 125
  • 1
  • 8
2
votes
1 answer

How to strip out leading time stamp?

I have some log files. Some of the UPDATE SQL statements are getting errors, but not all. I need to know all the statements that are getting errors so I can find the pattern of failure. I can sort all the log files and get the unique lines, like…
BWhite
  • 713
  • 1
  • 7
  • 24
2
votes
2 answers

How do I edit the values of imported CSV variable with PowerShell

The below script is an example of me importing a CSV file, trying to edit one of the values, then checking the value. $Animal_Farm = Import-CSV "Test.csv" Echo "The Data" $Animal_Farm Echo "`n`n`n" Echo "Dog's Status" $Animal_Farm.Status[1] Echo…
Baa
  • 438
  • 3
  • 7
  • 22
2
votes
1 answer

Powershell showing wrong output for JSON-object and ADDRESS-property

I am currently struggling to convert a JSON-string into an array of objects and to GENERALLY handle the properties/attributes of each object. Here is a simple demo, that shows that e.g. the attribute "address" seems to be a bit special: cls $json =…
Carsten
  • 1,612
  • 14
  • 21
2
votes
1 answer

How to add hashtable to multidimensional array? Cannot assign values via member-access enumeration

I'm having trouble with adding hashtables to a multidimensional array. I coded the following: $Data = @{BIBs = @( @{$BIB = @{BIBName=$BIBName}, @{Standort = $Standort}, @{Bücher = @( @{BuchName = $BuchName; Autor =…
EDybck
  • 43
  • 1
  • 7
2
votes
2 answers

Powershell: null file always generated (output of Compare-Object)

The most popular answer for this question involves the following Windows powershell code (edited to fix a bug): $file1 = Get-Content C:\temp\file1.txt $file2 = Get-Content C:\temp\file2.txt $Diff = Compare-Object $File1 $File2 $LeftSide =…
2
votes
2 answers

Get-ChildItem.Length is Wrong

I am writing a recursive function that goes through a directory and copies every file and folder in it. The first check I have in the function is to see if the path passed in has children. To find this out, I use the following…
Lotzi11
  • 477
  • 1
  • 13
  • 26
1
vote
1 answer

Powershell - How to retrieve Object[] data

I'm trying to get some data from api request, but rank it's getting corrupted. I have this code in a file: $queryResult=Invoke-restmethod -Uri "https://aoe2.net/api/leaderboard?game=aoe2de&leaderboard_id=3&steam_id=76561198011209758" Write-Output…
1
vote
1 answer

PowerShell Invoke-RestMethod skips a returned null value when navigating through the array values. Ideas?

I am calling an API that returns 3 values in an object "tags", which has values for "tags.name" and tags.results". The values below are what is returned. But as I try to navigate through the values, I can see that the "null" value, the {}, was not…
1
vote
1 answer

How to enter press square brackets in PowerShell on JSON File

How can I query change value of the "type" string in my JSON file with PowerShell? I can't get to the "type" string. JSON file { "name": "b", "compatibilityLevel": 1400, "model": { "culture": "c", "dataSources":[ …
MatthewSQL
  • 33
  • 3
1
vote
2 answers

How to match any part of a property string using Where-Object

I have an array $array with many elements, an example looks like this: ProfileID : 100 UID : 17 Name : SharePoint Description : SharePoint Server Description Now I am trying to filter by Name property,…
LightningWar
  • 915
  • 1
  • 19
  • 35
0
votes
1 answer

Powershell command to get filesystem drive names - member-access enumeration

I am trying to fetch disk free space and used space in windows 2008 R2 enterprise edition. However the command fails to get the values. This command works fine in windows 2012 version. Get-PSDrive -PSProvider FileSystem will fetch below…
Pradeep Shanbhag
  • 437
  • 5
  • 8
  • 19