Invoke-sqlcmd output without headers

2.9k Views Asked by At

I know this question has already been asked but the answers don't quite fit the constrains i have So here we go again (sorry for that) :

I have this line in my PowerShell script :

$WITHOUTCLIENT = Invoke-Sqlcmd -h -1 -Database CM_00A -Query "Select Members.Name From CollectionMembers Members Join Collections Coll on Members.SiteID = Coll.SiteID Where  Coll.CollectionName = '_SCCM-Machine Sans Client'"

The problem being that although when i count the number of items in this collection, the result is accurate (i.e. $WITHOUTCLIENT.count ) but when i try to display any item in this collection, it keeps on displaying a bloody header (i.e. $WITHOUTCLIENT[0] )

Note that i DON'T want to use an external file to store the result and that i should use Invoke-Sqlcmd, not Sqlcmd

i'm begging for help Thank you

1

There are 1 best solutions below

0
On

If you execute query:

$WITHOUTCLIENT = Invoke-Sqlcmd -h -1 -Database CM_00A -Query "Select Members.Name From CollectionMembers Members Join Collections Coll on Members.SiteID = Coll.SiteID Where  Coll.CollectionName = '_SCCM-Machine Sans Client'"

You are receiving list of objects. Each object has one property, but it's still an object. To get 'flat' collection, use select with `-ExpandProperty' argument:

$WITHOUTCLIENT = $WITHOUTCLIENT | select -ExpandProperty Name