It could be something like this: (Add your logic to switch between tables and etc..)
$userInput = read-host "exception" $exceptArray = $userInput -split "," foreach($allowedSchema in $allowedArray){ if($exceptArray -notcontains $allowedSchema){ $sqlString = "select * from [" + $allowedSchema + "].[tableName]" $connection = New-Object System.Data.SQLClient.SQLConnection $connection.ConnectionString = "server=\;database=;trusted_connection=;" $connection.Open() $cmd = New-Object System.Data.SqlClient.SqlCommand $cmd.Connection = $connection $cmd.CommandText = $sqlString $resultSet = $cmd.ExecuteReader() } }