I Have an array called $a

echo $a gives:

url                                                               load                                                             
---                                                               ----                                                             
win-coll2.astest.org                                              9                                                                
win-coll.astest.org                                               110                                                              
win-coll4.astest.org                                              110                                                              
win-coll5.astest.org                                              140                                                              
win-coll3.astest.org                                              410 

now i want to know, if the array contains a sting eg: win-coll2.astest.org should lead to true and win-coll7.astest.org should lead to false

is there a proper way to do this without a foreach?

with foreach:

$IsInArray = false
$ProofString = "win-coll2.astest.org"
foreach( $item in $a){
  if($item.url -eq $ProofString){
    $IsInArray = true

In powershell v4 (and possibly v3) you can do the following:

[PS] > $a.url -contains 'win-coll2.astest.org'
[PS] > $a.url -contains 'win-coll7.astest.org'

Otherwise, I think the easiest, least painful way is either do a foreach or a foreach-object...

| improve this answer | |
  • since which WinServ is PS v4 supported? Is it available on 2008? – Joel Dec 19 '14 at 15:38
  • I think you need R2, take a look at microsoft.com/en-us/download/details.aspx?id=40855 for more info about it. – Hunter Eidson Dec 19 '14 at 15:42
  • 1
    @Joel PoSh v4, through the Windows Management Framework v4 is available on Server 2008 R2, but not Server 2008. – HopelessN00b Dec 19 '14 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.