powershell arrays add-member - looking for elegant code

powershell array add
powershell array foreach
powershell multidimensional array
powershell array of objects
powershell array of strings
powershell add object to array
powershell clear array
powershell arraylist foreach

I have a pretty basic PowerShell array: $TestArray with 2 text columns: Price and TimeStamp (that's the way I get them, nothing to be done about this):

Price  TimeStamp       
-----  ----------------       
0.0567 1542056680.72746
0.0567 1542056650.34414
0.0555 1542056197.46668
0.0551 1542056167.28967

I would like, in a single PowerShell line to add a rounded Time2 value

$Time2 = [math]::Round($TestArray.TimeStamp)

The code I am thinking of is:

 $TestArray | Add-Member -Name Time2 -MemberType NoteProperty -Value { [math]::Round($Table.TimeStamp) }

Of course, I can do a ForEach loop; it would take care of it easily, but I would like to achieve this in this single line of code.

Any idea ?

Cheers, Philippe

PowerShell Array Guide: How to Use and Create, Learn how to best use PowerShell arrays — we'll cover the basics of how to Then we'll look at several different types of arrays, and explore some of Arrays are a common feature of almost all programming languages. An empty array is not that useful, however, so let's add some fruits to our new array. PowerShell is built using the .NET language where everything is represented as objects. Add-Member is a core cmdlet of the PowerShell language – it’s been there from the beginning, and it’s THE way to ADD a property to an object. It gets far less attention than other cmdlets that perform getting, sorting, filtering and formatting.

Alternatively you can achieve the same with Select-Object and a custom property:

$TestArray | Select-Object *,@{ n='Time2';e={ [math]::Round($_.TimeStamp) }}

Everything you wanted to know about arrays, Let's take a close look at arrays and everything they have to offer. Array addition; Plus equals +=; Pipeline assignment programming languages before I shift into the other ways PowerShell makes use of them I commonly pipe objects to Get-Member to learn more about them. Theme by beautiful-jekyll. By default, an array in PowerShell is created as a [PSObject[]] type. This allows it to contain any type of object or value. This works because everything is inherited from the PSObject type. Strongly typed arrays. You can create an array of any type using a similar syntax.

Change the member type to ScriptProperty and refer to the individual array item as $this:

$TestArray |Add-Member Time2 -Value { [math]::Round($this.Timestamp) } -MemberType ScriptProperty

Worth noting that in this example, the pipeline itself acts as a foreach loop, unravelling the array and binding each individual item to Add-Member

Using PowerShell ArrayLists and Arrays [Tutorial], Build Better Scripts with PowerShell ArrayLists and Arrays Arrays are used in many different programming languages and PowerShell is no different. To add elements to an existing collection, you can use the += operator or the Add cmdlet and CSS to create a beautiful HTML report with PowerShell! Arrays aren't just relegated to storing strings as shown above. You can also create arrays with other object types like integers. If you need an array of integers in sequential order, you can take a shortcut and use the range.. operator. Below you can see an array was created with the integers 2 through 5 with a single line of code.

Arrays in PowerShell – Create, change, read, sort, delete – 4sysops, Like any other scripting language, PowerShell supports the array Work with data types; Assign values to an array; Add elements to an An elegant alternative for assigning consecutive numerical values is As in other programming languages, you can access a specific Member Leaderboard – Year. How-to: Create and use PowerShell Arrays. A PowerShell array holds a list of data items. The data elements of a PowerShell array need not be of the same type, unless the data type is declared (strongly typed). Creating Arrays . To create an Array just separate the elements with commas.

Conditionally adding entries inside Array and object literals, About | Donate | Subscribe | Search | Archive | Books This blog post shows how you can conditionally add elements inside Array literals and The following code shows how a boolean cond determines whether or not the An elegant and self-descriptive solution is to use push() to construct the Array: In yesterday’s Hey, Scripting Guy! Blog, Learn Simple Ways to Handle Windows PowerShell Arrays, I discussed creating arrays, indexing into arrays, and two techniques for walking through an array. Working with specific array elements. One of the interesting things about arrays in Windows PowerShell is they are able to hold different data types.

Objects In PowerShell For Microsoft, The Powershell Mantra. Try not to use 1) Adding array objects via the += Operator ArrayList with New-Object and Add-Member PS modules. PowerShell provides a data structure, the array, which stores a fixed-size sequential collection of elements of the any type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables or objects.

Comments
  • why the single line of code requirement? [frown] a foreach loop is often far faster than anything that uses the pipeline.
  • Saving code length to add clarity for further maintenance (on this example, in real life, where I have no performance issues, I save overall 1 full page of code)
  • thanks for the rationale. [grin] it's wrong, but that is OK. take a look at the code posted by mklement0 for a thorough refutation of "too long". as for maintainability ... laying the steps out clearly is FAR more maintainable than a more-than-simple pipeline. ///// still, as my papa said "if we were all the same, the world would be boring as heck!" [grin]
  • Very clear and useful comments Manuel. I'll finally go with your last alternative, because I need to do further comparaisons and maths on my TimeStamp value - therefore it is preferable to have it as static NoteProperty type.
  • I had tried with $this but without changing the -MemberType to ScriptProperty !