GAM Script for Google Contacts

2,484 views
Skip to first unread message

Kristina Sedbrook

unread,
Oct 13, 2020, 1:42:11 PM10/13/20
to GAM for G Suite
Is there a GAM script to move Google contacts from one user account to another user account?

Kristina Sedbrook

unread,
Oct 13, 2020, 1:57:48 PM10/13/20
to GAM for G Suite
Or a way to export a user's Google Contacts into a CSV file?

James SEYMOUR

unread,
Oct 14, 2020, 12:39:18 AM10/14/20
to GAM for G Suite
Hi Kristina,

This should work, although if the contacts have lots of details you need to copy across then you will need to add more options.

Create a Google Sheet of the users contacts using gam user <User Email to Copy> print contacts todrive. This is in effect an 'export'.

If you only want to transfer the name & email address you canuse this command gam csv gsheet <gSheet Owner Email> <File ID> <Sheet Name> gam user <New User Email> create contact name "~Name" email work ~'Emails.1.address' primary. The command will create a contact for each row of the Google Sheet using ~Name column as the name and ~'Emails.1.address' column as the primary email address. You will need to add other options if you want to copy things like phone numbers and addresses etc.


James


Mohamed Lrhazi

unread,
Aug 12, 2022, 9:06:32 AM8/12/22
to GAM for Google Workspace
Two years later, I wonder if there is a way, or someone wrote a script, to perform this task... copy all contacts from one user account to another?

Thanks a lot,
Mohamed.

Ross Scroggs

unread,
Aug 12, 2022, 9:58:39 AM8/12/22
to google-ap...@googlegroups.com
Mohamed,

Test this with Advanced GAM:
gam redirect csv ./UserAContacts.csv user us...@domain.com print contacts selectcontactgroup "All Contacts" allfields formatjson quotechar "'"
gam csv ./UserAContacts.csv quotechar "'" gam user us...@domain.com create contact json "~JSON"

The characters after quotechar are " ' "

Ross
--
You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/191ae2ac-9478-4178-b563-3a7b22784268n%40googlegroups.com.

Mohamed Lrhazi

unread,
Aug 12, 2022, 12:51:04 PM8/12/22
to GAM for Google Workspace
Thanks Ross! The first command seems to be very heavy on Google! it does not pull all our our domain directory right? 
It keeps retrying and failing:

Temporary error: quotaExceeded - Quota exceeded for quota metric 'Critical read requests (Contact and Profile Reads)' and limit 'Critical read requests (Contact and Profile Reads) per minute per user' of service 'people.googleapis.com' for consumer 'project_number:59357090434'., Backing off: 16 seconds, Retry: 4/10



Mohamed Lrhazi

unread,
Aug 13, 2022, 12:05:57 PM8/13/22
to GAM for Google Workspace
Thanks for the help Ross. As you suggested, the following seems to have worked for me:

1- Export the contacts from the source account
2- Export the contact groups (labels) from the source account
3- Create the contact groups in the target account
4- Export the contact groups (labels) from the target account
5- Fix the contact group IDs in the contacts export from step 1
6- Import the fixed contacts into the target group.

I wrote two scripts to help automate this, though they are not very clean, nor very tested :

Bash script: 
Python script for fixing contact group IDs: 

Thanks,
Mohamed.

Ross Scroggs

unread,
Aug 13, 2022, 1:10:28 PM8/13/22
to google-ap...@googlegroups.com
Mohamed,

Get 6.25.12

In the print contacts command you can drop config csv_output_row_drop_filter "resourceName:regex:otherContacts" and add selectmaincontacts after print contacts
This will speed things up.

gam redirect csv /data/president.csv print contacts selectmaincontacts ...

Send full command if you'd like me to verify.

Ross

Mohamed Lrhazi

unread,
Aug 13, 2022, 4:20:10 PM8/13/22
to google-ap...@googlegroups.com
Yes, that worked! Thanks a lot!

gam redirect csv $CONTACTS_FN_SRC user $USER_SRC print contacts selectmaincontacts allfields formatjson quotechar "'"



Mohamed Lrhazi

unread,
Aug 13, 2022, 5:15:59 PM8/13/22
to GAM for Google Workspace
I noticed that if I run the "import" command twice, I get the contacts created twice.... Would there be an easy way to avoid re-creating a contact it it already exists?

Ross Scroggs

unread,
Aug 13, 2022, 6:05:15 PM8/13/22
to google-ap...@googlegroups.com
No. Google is quite happy to have N identical contacts. You'd have to pick a key field(s) and export the current contacts. Then, in a script, drop any import items with the same key field(s) value.

Christian Newman

unread,
Jan 11, 2024, 6:21:14 PM1/11/24
to GAM for Google Workspace
This is the "export" command, correct?

If so, what's the import command? I lost it in the back-and-forth with Ross.

Reply all
Reply to author
Forward
0 new messages