Use the -contains operator:
Copy$InputArray -contains $UserInput
With more recent PowerShell versions (v3 and above) you could also use the -in operator, which feels more natural to many people:
Copy$UserInput -in $InputArray
Beware, though, that either of them does a linear search on the reference array ($InputArray). It doesn't hurt if your array is small and you're not doing a lot of comparisons, but if performance is an issue using hashtable lookups would be a better approach:
Copy$validInputs = @{
'a' = $true
'e' = $true
'i' = $true
'o' = $true
'u' = $true
'1' = $true
'2' = $true
'3' = $true
'4' = $true
'5' = $true
}
$validInputs.ContainsKey($UserInput)
Answer from Ansgar Wiechers on Stack OverflowUse the -contains operator:
Copy$InputArray -contains $UserInput
With more recent PowerShell versions (v3 and above) you could also use the -in operator, which feels more natural to many people:
Copy$UserInput -in $InputArray
Beware, though, that either of them does a linear search on the reference array ($InputArray). It doesn't hurt if your array is small and you're not doing a lot of comparisons, but if performance is an issue using hashtable lookups would be a better approach:
Copy$validInputs = @{
'a' = $true
'e' = $true
'i' = $true
'o' = $true
'u' = $true
'1' = $true
'2' = $true
'3' = $true
'4' = $true
'5' = $true
}
$validInputs.ContainsKey($UserInput)
I understand that you are learning but you need to use Google-fu and the documentation before coming here.
Copy$inputArray -contains $userinput
Videos
After having initialised an array of arrays, I wanted to check whether if this array of arrays contained a specific array or not. So, I tried this:
$array = (1, 2), (3, 4), (5, 6) ($array -contains (1, 2))
Unfortunately, that returned "False", for some reason. I then tried to use a foreach loop to solve this, with this setup:
$array = (1, 2), (3, 4), (5, 6)
foreach ($item in $array)
{
($item -eq (1,2))
}but the comparisson of the two arrays seems to return absolutely nothing, and I don't get why. However, doing something like this:
$array = (1, 2), (3, 4), (5, 6) ($array -contains $array[0])
works mighty fine, despite of $array[0] and (1, 2) being essentially the same in function.
Please provide me with some enlightenment about this issue.
Greetings,
I am trying the following PS but it is not working. In other words, it is not returning any value to list
$Offices = @('India','Guatemala')
$List = Get-MsolUser -All | Where-Object {$_.Office -Contains $Offices} | Select UserPrincipalName
But if I try with just one value, it works
$Offices = @('India')
$List = Get-MsolUser -All | Where-Object {$_.Office -Contains $Offices} | Select UserPrincipalName
So, what am I doing wrong on the $Offices array? Is there another way to do this?
Thanks in advance
Evidently, PowerShell implicitly 'unboxes' a single-item array to a single object,
And zero item results to $null.
How can I prevent this from happening?
You can't.
How do you deal with this?
Use the array constructor (@(...)) to force a collection (possibly with zero or one elements) return:
$res = @(ls | %{$_.Name} | ?{$_.Contains("Prog")})
Note the difference between these two results:
PS C:\> ConvertTo-Json -InputObject @(1)
[
1
]
PS C:\> @(1)|ConvertTo-Json
1
PS C:\>
The point is that the 'unboxing' is being done by the pipe operation. ConvertTo-Json still sees the object as an array if we use InputObject rather than piping.