How to user SQL Server Powershell to run a query and return the results to Powershell script scope?

Hi Experts,

I am try to write a Powershell script to:
1. run on my desktop and query a remote database; and
2. return the rows to the scope of Powershell script for analysis

I wrote a test script using SQL Server Powershell as below
Set-ExecutionPolicy Bypass
Import-Module “sqlps” -DisableNameChecking
Invoke-Sqlcmd -Query "select top 10 * from schemaname.tablename" -ServerInstance "instancename" -Database "dbname" -Username "username" -Password "password"

Open in new window

It successfully connects to database and run the query, but displays the query result on the screen in some text format. How can I retrieve the result in some kind of objects in Powershell, such that I can do some analysis such as
Count the number of records?
Check some field values of some records?

From MSDN documentation Invoke-Sqlcmd I can only find that the output can be redirected to stderr. With some trick it maybe to some text file. However, this is not convenient. Is there any other possibility to get the returned data stored in nice collection objects in Powershell?

Thank you!

Below is the environment I am working with:
Remote database: SQL Server 2012
Powershell version on desktop: 4.0
SQL Server Management Studio on desktop: 2014
Desktop OS: Win 7
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBAduck - Ben MillerPrincipal ConsultantCommented:
In your code, you need to assign it to a variable and then you can manipulate it.

Set-ExecutionPolicy Bypass
Import-Module “sqlps” -DisableNameChecking
$results = Invoke-Sqlcmd -Query "select top 10 * from schemaname.tablename" -ServerInstance "instancename" -Database "dbname" -Username "username" -Password "password"
foreach($item in $results) {

Open in new window

Then you can check items or do anything with the results.  It is all about saving the results to a variable with $abc = something and you now have the results in your scope.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
huangs3Author Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.