Count all Salesforce records of all object types owned by a specific user

It’s not the fastest or most elegant script, but using the Force.com CLI it’s pretty easy to get a count of objects (of all types) owned by a specific user. It assumes that the field to be checked is ownerid, though, so might not work if you have other fields. A slightly better way might be to check which objects have an ownerid field first.

$ErrorActionPreference = ‘SilentlyContinue'
$objects = force sobject list
$ownerId = ‘00590000001Abcd’ # obviously replace this

foreach($object in $objects)
{
$resp = (force query –format json “select count(id) from $object where ownerid='$ownerId’” | convertfrom-json | select -ExpandProperty expr0)
$count = 0
if([Int32]::TryParse($resp, [ref]$count) -and $count -gt 0)
{
“$object : $count"
}
}