Invoke-Sqlcmd returning unwanted columns such as RowError, HasErrors

I’ve been performing some SQL queries recently using PowerShell and Invoke-SqlCmd.  Here is a simple example of returning a list of devices (a single column of data) from a database:

Of course I pipe this into Export-CSV like so:

When we read the exported data in the CSV we expect one column of data called ‘Device’.  However, instead we can also see columns called RowError, HasErrors and others!  And whilst I’m not sure why these appear, we can omit them like so:

and of course if we wanted to pipe this into a CSV we can do so like this:

And voila.  We only get the columns of data that we asked for!

Checking if System.Object[] contains a value

This isn’t the most interesting of blog posts admittedly.  But I was using the SQL Server Management Objects in my PowerShell script to return a result set from a stored procedure in SQL server.  The data set returned from the Invoke-sqlcmd command was a System.Object[] type.  I wanted to see if a column (the Department column) in the result set contained a specific value. So firstly I obtained the result set like so:

In PowerShell 3 it was a trivial exercise:

however in PowerShell 2 it required slightly more legwork, and was a little slower to run: