How to have index variable increase in ForEach-Object?

I have this strange observation: I want to run an expression for as many times as there are files. Yet, the following script always executes only once, no matter how many files are found by Get-ChildItem: (Get-ChildItem -Filter ‘*.png’), (Get-ChildItem -Filter ‘*.jpg’) | Sort-Object -Property ‘Name’ | ForEach-Object -Begin { $idx = 0 } -Process… Read More How to have index variable increase in ForEach-Object?

PowerShell – How can I use regex in the middle of registry file path to perform remove-item operation?

I’ve looked at many similar questions and tried lots of things, but I can’t make it work. $Regex = ‘RegexPattern’ Remove-Item -Path ‘HKCU:System\somePath\’ + $Regex + ‘\MorePath\*’ -Recurse Remove-Item -Path "HKCU:System\somePath\$Regex\MorePath\*" -Recurse Remove-Item -Path "HKCU:System\somePath\$($Regex)\MorePath\*" -Recurse Remove-Item -Path "HKCU:System\somePath\’RegexPattern’\MorePath\*" -Recurse Remove-Item -Path ‘HKCU:System\somePath\"RegexPattern"\MorePath\*’ -Recurse None of those work. I have a regex, want to delete… Read More PowerShell – How can I use regex in the middle of registry file path to perform remove-item operation?