SimonS
SimonS

Reputation: 1973

How to iterate over an array created by select-object

I just feel really stupid right now. I forgot how to iterate over an array created by Select-Object, and I can't find a solution.

I have a command like this:

$sitepublish = gci "W:\PublishSettings\$remoteIIS" | select FullName,BaseName

Which returns objects like this:

@{FullName=\\server\wdp$\PublishSettings\xy.PublishSettings; BaseName=xy}

How do I know iterate over it to get the FullName and the BaseName property for each object?

I tried this:

$sitepublish.Keys | % {
    Write-Host $_.FullName -fore Blue
    Write-Host $_.BaseName -fore Green
}

But it doesn't return anything.

Instead of .keys I also tried .properties, .psobject.properties, .getenumerator() and also just the variable without anything else (so just $sitepublish | % {}).

Upvotes: 1

Views: 1411

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

You can use the Foreach-Object cmdlet:

$sitepublish | Foreach-Object {
    Write-Host $_.FullName -fore Blue
    Write-Host $_.BaseName -fore Green
}

Upvotes: 1

Related Questions