Using an existing connection variable ($cm) you could try the following to retrieve unassigned numbers:
PS > $cm | Get-CMDirectoryNumber -Full | ? { $_.AssociatedDevices -eq $null }
The 'Full' parameter joins relationships on the data (i.e. 'AssociatedDevices'). Returned object(s) may be stored in a variable ($dn) which can be used to perform some action. For example, export the results and purge the unassigned numbers.
PS > $csv = "UnassignedNumbers.{0}.csv" -f [datetime]::now.ToString("yyyyMMdd")
PS > $dn | Export-Csv $csv
PS > $dn | % { $cm | Remove-CMDirectoryNumber $_.Name $_.Partition }
Optionally improve performance to retrieve unassigned patterns for large deployments.
PS > $cm | Get-CMSqlData "select numplan.dnorpattern Number, routepartition.name Partition from numplan left join devicenumplanmap on devicenumplanmap.fknumplan = numplan.pkid left join routepartition on numplan.fkroutepartition = routepartition.pkid where devicenumplanmap.fkdevice is null"