Windows – How to get PowerShell to read line by line and pass it into another command

command linepowershellwindows

A lot of the questions around the internet for this specific topic are oriented into BASH for linux, I was wondering if the same thing can be achieved using just PowerShell.

On Bash, one can use:

for line in $(cat file.txt); do echo $line; done

And then bash will loop through each line of file.txt and send it to echo or whatever command you're using.

How can the same thing be achieved on Windows's powershell?

I found that PS has the "Get-Content" command that works similarly to "cat", but I couldn't continue from there.

Best Answer

Ok it turns out that it was not that difficult but I'll leave it here for the future people of the internet.

PowerShell has a defined set of Loops, one of them called ForEach
One can simply do:

ForEach ($line in Get-Content [File]) {[Command]) $line}

Where [File] is the file path you're reading from and
Where [Command] is the command you're sending each line into.

Just as an example:

ForEach ($line in Get-Content thingstoecho.txt) {echo $line}
Related Question