r/PowerShell • u/metekillot • Apr 12 '26
Script Sharing PowerShell LinqLinker: Use System.Linq.Enumerable method query syntax in PowerShell -- built as a JIT-friendly struct for best performance; includes source-generated instance methods for every Enumerable method
https://github.com/Metekillot/LinqLinker
https://www.powershellgallery.com/packages/LinqLinker/1.0.6
using namespace System.Collections.Generic
using namespace System.Linq
[string[]]$StringArray = @("hello",",","world!")
[object[]]$ObjectClutter = @(1,2,3,"foo",[pscustomobject]@{name="bar"})
[int[]]$LotsOfNumbers = 1..10000
$StringLinq = [LinqLinker]::Link[string]($StringArray)
$ObjectLinq = [LinqLinker[object]]::new($ObjectClutter)
$IntLinq = [LinqLinker[int]]::Link($LotsOfNumbers)
$StringFunc = [System.Func[string,string]]{$s=$args[0];"selected -> $s"}
$($StringLinq.Select($StringFunc))
Write-Output ("`n"+"OfType check"+"`n")
$ObjectLinq.OfType[string]()
Write-Output ("`n"+"Numerical performance"+"`n")
[System.Func[int,int]]$IntOperation = {[int]$i = $args[0]}
$Standard = (Measure-Command { $LotsOfNumbers | ForEach-Object { $IntOperation.Invoke($_) } }); Select -InputObject $Standard @{Name='Name';E={'Standard'}},Ticks
$LinqLinkInt = (Measure-Command { $IntLinq.Select[int]($IntOperation) }); Select -InputObject $LinqLinkInt @{N='Name';E={'LinqLink[int]'}},Ticks
selected -> hello
selected -> ,
selected -> world!
OfType check
foo
Numerical performance
Name Ticks
---- -----
Standard 318901
LinqLink[int] 109493
17
Upvotes
View all comments
Show parent comment
2
u/Thotaz Apr 12 '26
I explained it in the previous comment:
If you write:
if ($Var = 42)it looks like the intent is to check if the value equals 42. If you don't know the person's skill level you have to guess if it's a noob that made a mistake, or a pro that tried to be clever.Another problem for PowerShell is the way it handles certain values. I mean in OPs case it acts like a null check, but what if the value on the RHS had been a string, number, or bool? Then it would be both a null and value check because PowerShell will treat certain values like $false
The average skill level in PowerShell is also quite low. What's the percentage of users who are aware that assignment statements can produce output in certain cases? Even if we only count the users who'd participate in a community like this one I'd estimate less than 5%.