Ironman Software Forums
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
LINQ is a set of methods that are useful in .NET for working with arrays of objects. Using LINQ is difficult in PowerShell because it doesn’t work well with generic methods. plinqo provides methods that accept script blocks so LINQ methods are easily accessible in PowerShell.
You can learn how plinqo is developed by visiting our GitHub repository.
plinqo is on the PowerShell Gallery and can be installed with Install-Module
. You’ll need to import the module with Import-Module
to get the plinqo
class defined.
Install-Module plinqo
Import-Module plinqo
plinqo exposes a single plinqo
class that you can pass your array into. Once in the plinqo
class you can take advantage of methods like Where
, First
, and Skip
.
$Array = 1..1000000
[plinqo]::new($Array).Where({$args[0] -gt 10000}).Skip(10).Take(100)
plinqo offers 20 different LINQ methods. In these examples we’ll be working with a simple array of 1 million integers.
$Array = 1..1000000
Returns $true
if all the items in the array match the filter.
[plinqo]::new($Array).All({$args[0] -lt 2000000})
Returns $true
if any of the items in the array match the filter.
[plinqo]::new($Array).Any({$args[0] -eq 3000})
Returns the average of the values in the array.
[plinqo]::new($Array).Average({$args[0]})
Combines two arrays together.
$Array2 = 1..1000
[plinqo]::new($Array).Concat($Array2)
Counts the number of items in the array.
[plinqo]::new($Array).Count()
Removes duplicates within the array.
$A = @(1,1,1,2,3,4)
[plinqo]::new($A).Distinct()
Returns the element at the index specified.
[plinqo]::new($Array).ElementAt(4)
Returns items that do not match the specified array.
$Array2 = 1..10000
[plinqo]::new($Array).Except($Array2)
Returns the first item and throws an exception if the list is empty.
[plinqo]::new($Array).First()
Returns the first item or $null
if the array is empty.
[plinqo]::new($Array).FirstOrDefault()
Returns the last item and throws an exception if the list is empty.
[plinqo]::new($Array).Last()
Returns the last item or $null
if the array is empty.
[plinqo]::new($Array).LastOrDefault()
Maps the items in the array and returns a new item based on the script block.
[plinqo]::new($Array).Select({$args[0] * 2})
Returns a single item and throws an exception if the array contains more than one item.
[plinqo]::new($Array).Take(1).Single()
Returns a single item or returns $null
if the array is empty.
[plinqo]::new($Array).Take(1).SingleOrDefault()
Skips the specified number of items in the array.
[plinqo]::new($Array).Skip(100000)
Sums the items in the array
[plinqo]::new($Array).Sum()
Takes the number of items specified in the array.
[plinqo]::new($Array).Take(20)
Filers the array based on the script block.
[plinqo]::new($Array).Where({$args[0] -gt 999999})
plinqo is pretty fast. It’s about as fast as the where()
method in PowerShell and much faster than Where-Object
.
$Array = 1..1000000
Measure-Command {
[plinqo]::new($Array).Where({$args[0] -gt 10000})
}
<#
Days : 0
Hours : 0
Minutes : 0
Seconds : 1
Milliseconds : 766
Ticks : 17665031
TotalDays : 2.04456377314815E-05
TotalHours : 0.000490695305555556
TotalMinutes : 0.0294417183333333
TotalSeconds : 1.7665031
TotalMilliseconds : 1766.5031
#>
Measure-Command {
$Array.Where({$_ -gt 10000})
}
<#
Days : 0
Hours : 0
Minutes : 0
Seconds : 1
Milliseconds : 632
Ticks : 16328007
TotalDays : 1.889815625E-05
TotalHours : 0.00045355575
TotalMinutes : 0.027213345
TotalSeconds : 1.6328007
TotalMilliseconds : 1632.8007
#>
Measure-Command {
$Array | Where-Object {$_ -gt 10000}
}
<#
Days : 0
Hours : 0
Minutes : 0
Seconds : 5
Milliseconds : 303
Ticks : 53037798
TotalDays : 6.13863402777778E-05
TotalHours : 0.00147327216666667
TotalMinutes : 0.08839633
TotalSeconds : 5.3037798
TotalMilliseconds : 5303.7798
#>
Find this useful? Please consider sharing this article. Have a question about PowerShell? Contact us and we'll write a post about it.
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
Receive once-a-month updates about Ironman Software. You'll learn about our product updates and blogs related to PowerShell.