13

I am attempting to create an audit of mailbox permissions in powershell and would like to remove specific accounts from the output within the powershell script rather than manually afterwards.

In order to do this I am looking for a way to compare the contents of an array against a single string in powershell.

For example, if I was to declare an array:

$array = "1", "2", "3", "4"

I then want to find a way to do something like the below:

$a = "1"
$b = "5"

if ($a -ne *any string in $array*) {do something} #This should return false and take no action

if ($b -ne *any string in $array*) {do something} #This should return true and take action

I am at a loss for how this would be accomplished, any help is appreciated

2

2 Answers 2

32

You have several different options:

$array = "1", "2", "3", "4"

$a = "1"
$b = "5"

#method 1
if ($a -in $array)
{
    Write-Host "'a' is in array'"
}

#method 2
if ($array -contains $a)
{
    Write-Host "'a' is in array'"
}

#method 3
if ($array.Contains($a))
{
    Write-Host "'a' is in array'"
}

#method 4
$array | where {$_ -eq $a} | select -First 1 | %{Write-Host "'a' is in array'"}
Sign up to request clarification or add additional context in comments.

Comments

0

Or...

[Int32[]] $data        = @( 1, 2, 3, 4, 5, 6 );
[Int32[]] $remove_list = @( 2, 4, 6 );

$data | Where-Object { $remove_list -notcontains $_ }

(returns 1, 3 and 5)

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.