Given a simple generic list of strings like this
$log = [System.Collections.Generic.List[String]]::new()
$log.Add('?_Label')
$log.Add('=_Item')
I can replace prefixes with something like this
$log | Foreach-Object {$_ -replace '^\?_', 'E_'}
But I would like to understand how to do this with the .ForEach()
method rather than the pipeline.
I have tried
$log.ForEach({$_ -replace '^\?_', 'E_' })
$log.ForEach({$arg -replace '^\?_', 'E_' })
$log.ForEach({param([String] $item) $item -replace '^\?_', 'E_' })
and none works.
I know from this that I need a delegate, and this in theory should tell me how to do it. But I am at a loss.
I also found this which is what sent me down the initial rabbit hole, but this is obviously a native PowerShell .ForEach()
method, where $_
is valid, and I am dealing with a native .NET method, where $_
does not apply. But I think there is even more that I am missing.