3

I have the following powershell query :

$sql = "select name, lastName, City, Address from Persons"

$TotalPeople = Invoke-Sqlcmd -ServerInstance $ResultsInstance -Database $ResultsDatabase -Query $sql

Write-Output "Total number of people run is $TotalPeople  "

I want to be able to count the number of rows in the $TotalPeople variable. Is there a way to do this in powershell?

1
  • 7
    $TotalPeople.count ? :) Commented Sep 7, 2017 at 15:32

1 Answer 1

9

Invoke-sqlCmd returns an array.

You can therefore simply use the Count property.

$sql = "select name, lastName, City, Address from Persons"
$TotalPeople = Invoke-Sqlcmd -ServerInstance $ResultsInstance -Database $ResultsDatabase -Query $sql
Write-Output "Total number of people run is $($TotalPeople.count)"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.