Post

Using ForEach() Method for Array Iteration

I learned about a nifty ForEach() powershell method when looping through an array. You can call it on a collection and pass a script block to it!

Context:

I was recently reading the new docs for GitHub copilot on how you can now use it in the CLI. GitHub launched the gh copilot explain and gh copilot suggest commands as part of the copilot extension in the GitHub CLI.

Example Problem:

How can I get an explanation of all of the sql files in a folder?

Example Solution:

1
2
$files = @($(Get-ChildItem -Path .\ -r -Filter *.sql))
$files.foreach{gh copilot explain $(Get-Content $PSItem)}

Refernces

This post is licensed under CC BY 4.0 by the author.