6

I am familiar with how BASH syntax handles this request but I am unable to find a way to do this in PowerShell.

BASH Example:

for x in `ls .`; do something with ${x}; done

What I am trying to perform the same thing with PowerShell (which has the incorrect syntax for demonstration purposes)...

ForEach ($group in `$wsus.GetComputerTargetGroups()`)

...I obviously get a syntax error.

BASH to PowerShell translation is the ask here. :D

2 Answers 2

7

For the example you have, it would just work (without the backticks):

ForEach ($group in $wsus.GetComputerTargetGroups()) {
    # do stuff
}

If it's a command, you can wrap it in a subexpression:

foreach ($process in $(Get-Process)) {
    # do stuff
}

You might also look at the ForEach-Object cmdlet which can be more idiomatic in PowerShell's pipelines:

Get-Process | ForEach-Object { Write-Verbose "This process is $_" -Verbose }
Sign up to request clarification or add additional context in comments.

Comments

0

This might help you get going...

$things = Get-ChildItem C:\Windows\

foreach ($thing in $things) {
    # Do a thing with each one in turn
    Write-Host $thing.Name -ForegroundColor Magenta
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.