A more efficient way to do it is to have Google perform the query on their end (remove "aliases" if you only need the primary address):
gam print users query email:
bar.com aliases
I usually want to be extra certain that I've captured all aliases, and I usually want to delete the aliases, so I typically let GAM query all users (and groups) via this command:
gam config csv_output_row_filter "Alias:regex:.*@
bar.com" print aliases
To delete all aliases at the old domain, I run this command:
gam print aliases | gam csv - matchfield Alias "@
bar.com$" gam delete alias ~TargetType ~Alias
Note that you may need to run that command multiple times to delete all aliases. If users have multiple aliases at the old "
bar.com" domain, gam will give an error as you can't delete multiple aliases at once (at least, not in the way gam is doing it in that command), so running the command multiple times will clear out these extra aliases. If some users have many aliases at the old domain (it's possible to have 25 or so aliases), then you can change the "gam csv" to "gam loop" for gam to run single-threaded, so you don't have to wait for gam to get a list of all 160k+ users more than is really necessary.
Also, I've found that the Google API is slow for deleting aliases, I seem to run into quota limits much more often when deleting aliases than I do for other Admin SDK quotas, even with Admin SDK quota limits higher than default. So if there are a lot of these to delete, be prepared for that.