Printed 2014/12/17 05:47PM

PowerShell and SQL – Easy and Different Ways to Find Number of Rows of all Tables in a database

By Prashanth Jayaram, 2013/04/11

SQL Example:

DECLARE @TableRowsCount table
Tablename varchar(50),
cnt int)

insert into @TableRowsCount
EXEC sp_MSforeachtable @command1=” select ‘?’,count(*) from ?”

select * from @TableRowsCount

PowerShell Example:

Load SQL PowerShell by typing “SQLPS” in PowerShell console


Change Directory to the desired database

PS SQLSERVER:\ cd SQL\HQDB001\default\databases\DummyDBName\tables

PS SQLSERVER:\SQL\HQDB001\default\databases\Powersql\tables> dir |format-table  Name,Rowcount

Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.