Return SQL Query as Array in Powershell

I'd do this:

$DBNameList = @(Invoke-SQLCmd -query "select Name from sysdatabases" -Server DEVSQLSRV) | select-object -expand Name

That will give you an array of names.The option -contains should work fine.


What's missing from the original post is some type of conversion, from an object to an array.

Powershell outputs the result of $DBNameList because it kinda interprets the object. But if you need to manipulate this object and identify a specific item from it, this is the method I use:

$Itm = "DBTwo"
$DBNameList = @(Invoke-SQLCmd -query "select Name from sysdatabases" -Server DEVSQLSRV)
$NameList = @($DBNameList | select-object -ExpandProperty Name)
$Name = ($NameList.Split()).Contains($Itm)
Write-Output $Name

True

I have been looking for this myself for a while and finally worked it out, so I hope it helps someone else!