If you haven't done this already, you can turn on the "Echo All Commands" option from inside the Script Editor (Script -> Echo All Commands).
I'm on Maya 4.5 and it came up with...
hyperShadePanelMenuCommand("hyperShadePanel1", "deleteUnusedNodes");
...when I did the "Delete All Unused" from Hypershade.
There is also the "File -> Optimize Scene Size" menu option. It deletes a lot of unused nodes.
If you want to do it yourself, you could also loop through all of the nodes in the scene and count how many connections each node has:
int $numConnections = size(listConnections $node
);
This will get you closer. But, be careful... Just because a node has 0 connections doesn't mean you can delete it. For instance: transform nodes. Often, they aren't connected to anything, but you don't want to go deleting them!