Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreach loop in Powershell on the console

Tags:

powershell

Very simple question here, I want to see how can we process a bunch of commands using foreach on the command line (not through a PS1 script).

For instance, I display the directory listing on the console, now I want to execute 2 commands per object.

Get-ChildItem | ForEach-Object { Write-Host $_ $_}

This is ok, it shows the filename twice, but lets say I wanted to run 2 Write-Host commands for the same object, would that be possible on the console?

PS: I'm trying to achieve writing an output to 2 files using the out-file cmdlet, so I can read something and have 2 separate out-file calls per object

Thanks

like image 214
NullPointer Avatar asked Jun 07 '13 14:06

NullPointer


1 Answers

you can script in the console windows just as you would in a powershell file. Use the "`" (backtick) key to separate lines. e.g.:

PS > Write-Host `
>>> hello, world!

So you could do

PS > Get-ChildItem | ForEach-Object { `
>>>    Write-Host $_ `
>>>    doFoo() `
>>>    doBar() `
>>> ` }
like image 109
ash Avatar answered Nov 15 '22 03:11

ash