1

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 | % {}).

1 Answer 1

1

You can use the Foreach-Object cmdlet:

$sitepublish | Foreach-Object {
    Write-Host $_.FullName -fore Blue
    Write-Host $_.BaseName -fore Green
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm already using this, % {} is an alias for foreach-object {} :)
You don't have to use the Keys property to iterate over it. Give it a try ;-)
it doesn't work, I already tried it without a property, and now again because you made me insecure. no return
Which PowerShell version you are using? This works fine for me.
i got it now. it was a problem of the powershell_ise itself. after restarting it worked. i guess it was because I played around with the $sitepublish variable. made it a [string[]], emptied it with $sitepublish = "", made it a [psobject], seems like ISE couldn't handle this, so that's where the error occured. thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.