Export every field of every record of a bunch of Salesforce objects

I needed to get a backup of every record of a bunch of objects (for posterity) which were about to be deleted, I wanted to grab every field and do it in one line. I cheated a little, because I’ve defined some functions that just do some of the basic processing. I could put the whole function definitions on one line too, but that’d be a bit contrived ;) They’re below for reference



(‘object_name_1__c’,‘object_name_2__c’) |
foreach-object {
$fields = (sffields $_) -join ‘,‘
$query = “select $fields from $_“
sfquery $query | export-csv “$_.csv” -NoTypeInformation -Encoding ASCII
}

function SFFields
{
(force describe -n="$($args[0])” -t=sobject | ConvertFrom-Json).Fields | Sort-Object name | Select-Object -ExpandProperty name
}

function SFQuery
{
force query –format json $args[0] | ConvertFrom-Json
}