Measure Statement Performance with PowerShell

When you monitor for performance, you might start in the database server itself. But a true test really has more to do with the “round trip” of a data request from a client to a server and then the return of data back to the client.

So I wired up this little test to simulate that process, and now I can run that from anywhere. The trick is the measure-Command { } part, which brings back the time display. In my example, I’m testing the backup process, but your command can be anything you like. I’m also using the ExecuteNonQuery call, since I don’t want any data back. You might want to use the ExecuteReader call if you do want data.


If you want to try this (and remember, test system only, read and understand, etc.), you need to change the SERVERNAME\INSTANCENAME to your system and instance names, and change the:

$sqlCommand.CommandText="BACKUP DATABASE TESTDATABASE TO DISK='c:\TEMP\TESTDATABASE.bak' WITH INIT" line to some command you want to run.

I’ve alternated the colors – keep all of one color on one line:


$sqlConnection = new-object System.Data.SqlClient.SqlConnection

$sqlConnection.ConnectionString = "server=SERVERNAME\INSTANCENAME;integrated security=true;database=master"


$sqlCommand = new-object System.Data.SqlClient.SqlCommand



Measure-command {$sqlCommand.ExecuteNonQuery()}