Gmail

Archive Messages

Archive messages from a User to a Group email address.

gam user <User Email Address> archive messages <Group Email Address> doit

Add Label

If you want to be able to identify the messages after the migration, use the matchlabel to add a labe to each email.

gam user <User Email Address> archive messages <Group Email Address> matchlabel <LabelName> doit

Delegates

View all Users delegate access

gam all users print delegates

View all Delegate Access for OU members

Find all the delegate access for an OU (e.g. Staff) and the sub-OUs for current users (ns = not suspended). The output is sent to Google Drive. This will only show the delegated accounts in this OU and the users with deleage access.

gam ou_and_children_ns <top level OU> print delegates todrive

Delegate Access

User

gam user <shared email address> delegate to <user email address>

CSV

Using a CSV file, where ~user is the list of emails for delegation

gam csv <file name>.csv gam user <shared email address> delegate to ~user

Group

When new users are added to the group they automatically get delegate access to the delegated mailbox

gam user <shared email address> delegate to <Group Email Address>

Check Delegates

View the delegates for a specific user account.

gam user <Shared Email Address> print delegates

If you want to see the Display names of the users, add shownames

gam user <Shared Email Address> show delegates shownames

Delete Delegates

gam user <shared email address> delete delegate <user email address>

Messages Sent

Show the count of the messages sent by each users from a Delegated mailbox, during a date range. If you remove the todrive option, you will need to view the contents of SentByDelegates.csv. Remove the countsonly so see more info on the messages, such as Subject, To & Date.

gam user <Delegated Email> print delegates | gam redirect stderr - multiprocess redirect csv ./SentByDelegates.csv multiprocess csv - gam user <Delegated Email> print messages query "in:sent after:<yyyy/mm/dd> before:<yyyy/mm/dd>" sendermatchpattern "~delegateAddress" countsonly todrive

Alternatively you can print the Delegates to the Delegates.csv using.

gam user <User Email Address> print delegates > Delegates.csv

This would be useful if you want to edit the Delegates.csv before using this command. See the note above regarding the countsonly & todrive options.

gam redirect csv ./SentByDelegates.csv multiprocess csv Delegates.csv gam user <Delegated Email> print messages query "in:sent after:<yyyy/mm/dd> before:<yyyy/mm/dd>" sendermatchpattern "~delegate" countsonly todrive

Delete Messages

Notes

Single User

gam user <user email address> delete messages query "rfc822msgid:<message ID>" doit

All Users

gam all users delete messages query "rfc822msgid:<message ID>" doit

Group of Users

gam group <Group Email Address> delete messages query "rfc822msgid:<message ID>" doit

OU and sub OUs

You could make this command run quicker if you spcify ou_and_children_ns, so that it only removes from non-suspended accounts. But then advisable to run without _ns after to fully remove the email.

gam ou_and_children </Top/Level/OU> delete messages query "rfc822msgid:<message ID>" doit

Delete Email sent from an account

For a single user

Add doit to the end of this command to change from testing to actually delete the emails. <Email to Delete> can be an internal or external address.

gam user <User Email Address> trash messages query "from:<Email to Delete> before:yyyy-mm-dd" maxtotrash 1000

For all users

gam all users delete messages query "from:<Email to Delete>"

For a User Account

Permanently Delete the messages

gam <user email address> delete messages

Or move them to the Trash

gam <user email address> trash messages

Deleting Message Older than 3 Months

<Action> = delete|trash|untrash

<Type> = messages | threads

gam user <user email address> <Action> <Type>  query "older_than:3m"

Messages from a specific Domain before a Date

To test use the command below. Add doit to the end when you want to remove the messages.

gam user <user email address> trash messages query "from:<Domain to Delete> before:<yyyy-mm-dd>" maxtotrash 1000

by CSV file

The CSV file needs to be edited so that the field contains the full Message ID, e.g. rfc822msgid:<message ID>. This is useful after searching for keywords in the emails which results in different users having different message-IDs.

gam csv <CSV File Name>.csv gam user ~User delete messages query "~Full-Message-ID" doit

by Google Sheet

The Google Sheet needs to have a column for the Users emails and the Message ID. Copy the Message IDs for all the users, if the Message ID is the same.

gam csv gsheet <user email address> <File ID> <Sheet Name> gam user ~User delete messages query "rfc822msgid:~~MessageID~~" doit

This command will update an existing sheet/tab of the Google Sheet used to run the command. It will list the users who have had an email deleted.

gam redirect csv - multiprocess todrive tdfileid <File ID> tdsheet id:<Sheet ID> tdupdatesheet tdretaintitle csv gsheet <File Owner Email> <File ID> <Sheet/Tab Name> gam user ~User delete messages query "rfc822msgid:~~MessageID~~" doit csv

By ID

This is the id shown when printing a summary of the emails.

gam user <User Email Address> delete messages ids <For Example 012a345b678c9,012d345e678f9>

For a Label and sub-Labels

Delete all the messages in a nested label. The Label name must be unique of all other messages will be deleted as well.

gam user <User Email Address> trash messages labelmatchpattern '<Top Label Name>'

Delete Messages (All)

Show all the messages a user has.

NB, there are 3 additional emails, created by Google when an account is created.

gam user <User Email Address> show messages query <Choose Option>

Query options:

The show command above will show  <Number> the number of messages to be deleted. The following command will process the GAM command and show the output on the screen. Add doit to actually delete the emails.

gam user <User Email Address> delete messages query <Choose Option> max_to_delete <Number>

Using CSV

Change delete to trash and max_to_delete to max_to_trash to move messages to the Bin. Messages deleted will bypass the bin.

gam csv <FileName.CSV> gam user ~<Email Field> delete messages query deliveredto:~~<Email Field>~~ max_to_delete <Number> doit

Filters

View a Users Filters

gam user <User Email Address> print filters todrive

Filter Info

The ID from the above command is used in this command to show the info for the filter or delete it.

gam user <User Email Address> info filters <FilterID>

Delete Filter

gam user <User Email Address> delete filters <FilterID>

Add Filter

Forward emails. The forwarding address needs to be already added to the email account.

gam user <User Email Address> add filter to <Messages to this Email> forward <Are sent to this Email>

gam user <User Email Address> add filter from <Messages from this Email> forward <Are sent to this Email>

OU and forward contains forward

Print users in an OU and sub-OUs, where the forward column contains the word forward

gam config csv_output_row_filter 'forward:regex:.*forward' ou_and_children </Path/To/Top/OU> print filters todrive

Forward Message/Threads

Options

MaxToForward
In most cases these commands will only forward a single email. Use the maxtoforward <Number> to specifiy the number of message to send above 1.

DoIt
Running a command without the doit option show the output but nothing will be forwarded. Add doit at the end to process the command and send the emails.

Subject
Adding the subject "<New Subject Text>" option to a command will replace the original subject with the specified text.

Query Subject

Use the search operators on this page https://support.google.com/mail/answer/7190?hl=en to formulate the query

gam user <User Email Address> forward message to <To Email Address> query "subject:<Gmail Subject Query>"

Message IDs

Use the show message option to find the message IDs. These are not the message IDs that include the @domain.com info. They are a alph-numerical ID, such as 12en45726sd7823.

gam user <User Email Address> forward message to <User Email Address> ids <ID One>,<ID Two>,<ID Three>,etc

Label

gam user <User Email Address> forward messages to <User Email Address> matchlabel '<Label Name>'

Forward Threads & 'Long' Message ID

The 'long' Message ID can be obtained from the message Meta data, using the 'Show Original' option in the email.  Example of these can be seen here https://sites.google.com/jis.edu.bn/gam-commands/services/gmail#h.auypqze3v0ju.

This command will allow you to easily forward an email and all threads to a new user.

gam user <User Email Address> forward threads to <User Email Address> query "rfc822msgid:<Message ID>"

Forwarding Address

Before a forwarding address can be used, it needs to be added. The forwarding address can then be used in email rules or for a 'blanket' forward. Once added the address needs to be 'Set' to activate the forwarding of mail. NB only 1 forwarding address can be added, others will need to be set up a rules/filters.

Show forwarding addresses

gam user <user email address> show forwardingaddress

To check all the users in an OU (or use ou_and_children_ns for users in sub-OUs not suspended)

gam ou </Path/To/OU> print forwardingaddresses todrive

Add forwarding address

gam user <user email address> add forwardingaddress <email address to forward to>

Delete forwarding address

gam user <user email address> delete forwardingaddress <email address to remove>

Remove all the forwarding addresses for a user

gam user <user email address> print forwardingaddresses | gam csv - gam user "~User" delete forwardingaddress "~forwardingEmail"

Set a forwarding address

The option at the end of the command can also be archive or delete. And an option command markread can be added.

gam user <user email address> forward on keep <email address to forward to>

Disable forwarding

gam user <user email address> forward off

Show forwarding status

gam user <user email address> show forward

OR, but no real need to have the todrive option.

gam user <user email address> print forward todrive

Show users with Auto-Forward enabled

gam redirect csv <CSV File Name>.csv ou_and_children_ns /<top level OU> print forwards enabledonly

In the Cloud the CSV file is saved in the Downloads folder. Alternatively, to save the file in you My Drive;-

gam ou_and_children_ns /<top level OU> print forwards enabledonly todrive tdtitle "<File Title>"

From a CSV of users. Assuming the header for the email column is email.

gam csv <CSV File Name>.csv gam user "~email" show forward

IMAP & POP

User Settings

Replace IMAP or POP as needed

gam user <User Email Address> show [imap|pop]

User enable/diable

Enable IMAP or POP (yes) or disable (no)

gam user <User Email Address> [imap|pop] [yes|no]

Ou Settings

gam ou_and_children_ns </Path/To/OU> print imap todrive

See Also

Users > Reports > IMAP/POP

Insert Message

Insert message will add an email into a users Inbox, without the message being added to the senders mailbox. If you want the emails to also appear in the senders mailbox, use the sendemail option.

For All Users

gam all users insert message subject "<Email Subject>" textmessage "<Email Message>" addlabel unread addlabel inbox

For a specific user

gam user <User Email Address> insert message subject "<Email Subject>" textmessage "<Email Message>" addlabel unread addlabel inbox

Message from Google Doc

Message to users in a OU and sub-OUs, from a specific person, where the text is taken from a Google Doc. NB Google Doc formatting is not shown in the message and links need to be written in full.

gam ou_and_children_ns </Path/To/Top/OU> insert message from <Sender Email Address> subject "<Subject>" gdoc <Doc Owner Email> <FileID> addlabel unread addlabel inbox

Message from HTML file

If you have created the message in Google Docs, with formatting, you will need to save this file as an HTML file

gam user <Your Email Address> get drivefile id <FileID> format html targetfolder </Target/Folder/Path> targetname <File Name>.html

The run this command in the same folder where the HTML file is saved.

gam ou_and_children_ns </Path/To/Top/OU> insert message from <Sender Email Address> subject "<Subject>" htmlfile <File Name>.html addlabel unread addlabel inbox

HTML Message from a Google Doc

Insert an HTML version of a Google Doc, so including the text formatting.

gam ou_and_children_ns </Path/To/Top/OU> insert message from <Sender Email Address> subject "<Subject>" ghtml <Doc Owner Email> <FileID> addlabel unread addlabel inbox

Labels

Add Label

gam user <User Email Address> add label "<Label Name>"

gam group <Group Email Address> add label "<Label Name>"

Add Label to non-suspended users

gam print users query isSuspended=False | gam csv - gam user ~primaryEmail label "<Label Name>"

Delete Label 

gam user <User Email Address> delete label "<Label Name>"

gam group <Group Email Address> delete label "<Label Name>"

Add a Filter and Label for sent messages

This command will also add the settings to mark the messages read and archived to the filter. It will only work on future messages.

gam user <User Email Address> add filter query "to:<Sender Email Address>" label "<Label Name>" markread archive

Update existing messages

Check the number of messages match a query

gam user <User Email Address> show messages query "in:chat" countsonly

Delete All Labels

gam user <User Email Address> delete labels --ALL_LABELS--

Label Message Counts

To see a list of all the labels for a user, including system labels.

gam user <User Email Address> print labels

Or to only see the user created labels, add useronly.

gam user <User Email Address> print useronly labels

To see an overall message count for each label, including system labels.

gam user <User Email Address> print labels showcounts todrive

Or, to only see labels created by the user, add useronly

gam user <User Email Address> print labels showcounts useronly todrive

All Label Message Count for a Date Range

It is not possible to use the showcounts option in a date range, so to find the message count for a lable, some Google Sheet functions are needed. For a single lable, run

gam user <User Email Address> show messages query 'after:yyyy-mm-dd before:yyyy-mm-dd label:<Lable Name>' countsonly

For all the lables (system & user), run

gam user <User Email Address> print messages query 'after:yyyy-mm-dd before:yyyy-mm-dd' showlabels headers subject todrive

This will produce a Google Sheet with the column headings User , threadId, id, Subject & Labels. In the Google Sheet, in cell F1 enter =unique(E:E), to create a list of the lables listed in Column E. Then in G2 (G1 is the heading row) enter =countif(E:E,indirect("F:F")) and drag this down to the end of the Unique Label list. The count of message for each label will be shown.

A similar example can be seen here, showing the count of employees in each department.

User Label Message Count for a Date Range

Add useronly to remove the system labels. The data will still need some Google Sheet functions to calculate the message count. See above.

gam user <User Email Address> print messages query 'after:yyyy-mm-dd before:yyyy-mm-dd' showlabels useronly headers subject todrive

Label Count for Date Range

Alternatively, to create a Google Sheet with the counts only, run this command. 

gam user <User Email Address> print messages query 'after:yyyy-mm-dd before:yyyy-mm-dd' showlabels countsonly useronly positivecountonly todrive

However the output, in 2 lines, is not that easy to work with. This Google Sheet runs through what you need to do. It uses the formula =ArrayFormula(VLOOKUP(SEQUENCE((MAX(ROW(A:A)*(A:A<>"")))/3,3,1),{ROW(A:A),A:A},2,0)) to convert the data in column format to an array. Please note that the 3's, used in the middle of the formula are becase the data in the column repeats every 3 rows.

Print All

gam user <User Email Address> print labels todrive

Print Selected

gam user <User Email Address> print labels labellist "'<Label 1>','<Label 3>','<Label 3>'"

Show  Nested

This command will show a users labels and the nested true option will indent the output.

gam user <User Email Address> show labels nested true

Update

gam user <User Email Address> update labelsettings "Label Name" backgroundcolor "<Background Hex Colour>" textcolor "<Tex Hex Colour>"

Modify

Remove Label from messages

The <Label Name> can be UNREAD.

gam user <User Email Address> modify messages query "label:<Label Name>" removelabel <Label Name> maxtomodify 1000000 doit

If removing one of the 'Category' labels (Social, Promotions, Updates or Forums) add category_ before the label name.

gam user <User Email Address> modify messages query "label:updates" removelabel category_updates maxtomodify 1000000 doit

Addlabel

gam user <User Email Address> modify messages query "from:<Sender Email Address>" addlabel "<Label Name>" maxtomodify 1000000 doit

Addlabel for messages after a date

gam user <User Email Address> modify messages query after:<yyyy/mm/dd> quick doit max_to_modify <Max Messages to Modify> addlabel "<Label Name>"

Mark messages Unread

Mark all message read (remove UNREAD label) for a user

gam user <User Email Address> modify messages query "is:unread" removelabel UNREAD max_to_modify 99999 doit

This commend will search for messages that are unread and also labelled with <Label Name>. Add doit to the end to make the changes.

gam user <User Email Address> modify messages query "is:unread and is:<Label Name> " removelabel UNREAD

Move All Messages from Inbox to New Label

After adding a new label, see above, run command below. You have to specify max_to_process N if you want a value other than 1.

max_to_process 0 means process all messages

gam user <User Email Address> modify messages query "label:Inbox" addlabel "<New Label Name>" removelabel "Inbox" max_to_modify 0 doit

Mark a message unread

gam user <User Email Address> modify messages query "rfc822msgid:<Message ID>" addlabel unread doit

Print GmailProfile

Print the number of messages and threads for users.

For users

gam user <User Email Address> print gmailprofile

For OUs

gam ou_and_children_ns <Path/To/OU> print gmailprofile todrive

gam ou <Path/To/OU> print gmailprofile todrive

For All Users

gam all users print gmailprofile todrive

Print Messages

Countsonly

Only number of emails

gam user <User Email Address> print messages countsonly

Summary As CSV

View a summary of a users emails in a CSV format.

gam redirect csv ./<CSV File Name>.csv user <User Email Address> print messages headers to,date,from,subject


Print Messages Matchlabel

Specific label

gam user <User Email Address> print messages matchlabel <Label Name> todrive tdtitle "<File Name>"

Specific label and are unread

gam user <User Email Address> print messages matchlabel <Label Name> and matchlabel unread todrive tdtitle "<File Name>"

Print Messages Query

User and Date Range

gam user <User Email Address> print messages query 'after:<yyyy-mm-dd> before:<yyyy-mm-dd>' includespamtrash showbody todrive showattachments

People who have not read a message

By Group

gam group <Group Email> print messages query "rfc822msgid:<Message ID>" matchlabel unread todrive

By OU

gam ou_and_children_ns <Path/To/OU> print messages query "rfc822msgid:<Message ID>" matchlabel unread todrive

For All Users

gam all users print messages query "rfc822msgid:<Message ID>" matchlabel unread todrive

Subject

This command requires \" to surround the search words to ensure that exact phrase is found. In practice Gmail requires exact searches to be surrounded by " ", however the \ is required in GAM to ensure that query does not consider the first " (of the search phrase) as the end of the query. Single quotes cannot always be used to group the whole query (e.g. query 'subject:"<Subject Text>"') as ' will not work with Windows systems.

To select messages with all of the words (word1, word2 and word3) in the subject, regardless of location or order, use:

query "subject:word1 word2 word3"

To select messages with exactly (word1 word2 word3) in the subject, use:

query "subject:\"word1 word2 word3\""

Add includespamtrash to show messages in Spam or Trash folders

gam user <User Email Address> show messages query "subject:\"<Subject Text>\""

gam user <User Email Address> print messages query "subject:\"<Subject Text>\"" todrive

Subject & Date Range

For active users in an OU and below, with a specific subject and within date range.

gam ou_and_children_ns </Path/To/Top/OU> print messages query "subject:\"<Subject>\" after:yyyy-mm-dd before:yyyy-mm-dd" todrive tdtitle "<File Tille>" tdtimestamp false

Subject, Date Range & From

gam ou_and_children_ns </Path/To/OU> print messages query "subject:\"<Subject>\" after:<yyyy-mm-dd> before:<yyyy-mm-dd> from:<Sender Email Address>" headers from,to,cc,bcc,subject,message-id,date todrive

Search Body

gam all users print messages query "\"<Text to Search For>\"" todrive tdtitle "<File Name>" tdtimestamp false

Showbody

Use the showbody option to display the contents of the message. Change the date range, to/from and enter subject text.

gam user <User Email Address> show message query "to:<Another Email Address> after:03/01/2017 before:05/01/2017 subject:\"<Subject Text>\"" showbody

Headers

Print specific headers.

gam user <User Email Address> print messages query "from:<Another Email Address> after:yyyy-mm-dd" headers User,threadId,id,Date,Subject,From,To,Delivered-To,Content-Type,Message-ID,List-ID,cc todrive

Large Emails

For example for messages larger than 10Mb use larger:10M.

gam user <User Email Address> print messages query "larger:<Mb in Numbers>M" includespamtrash todrive

Labels

gam user <User Email Address> print messages query "label:<Label Name>" todrive

Count of messages with the label and if the messages are read or unread.

gam user <User Email Address> print messages query 'label:<Label Name> is:[read|unread]' countsonly

To User or Group

Searches the To, CC and BCC fields.

gam user <User Email Address> print messages query "to:<To Email Address>" headers from,to,cc,bcc,subject showdate query "after:yyyy-mm-dd"

Domain

From

gam user <User Email Address> print messages query "from:<Domain to Search>"

To OR From

gam user <User Email Address> print messages query "to:<Domain to Search> OR from:<Domain to Search>"

Print Users

Email Address

Use wildcard (*) as needed in the search term. Use other parameters to fine-tune the search, for example include an OU and by default this will include all sub-OUs (I think). 

gam print users query "email:<Search Term>"

gam print users query "email:<Search Term> orgUnitPath:'</Path/To/OU>'"

SendAs

Specific sendAs Account

Users who can sendas a specific user account

gam config csv_output_row_filter "sendAsEmail:regex:<Email To Check>" all users_ns print sendas todrive

Single User

gam user <User Email Address> print sendas todrive 

All Users

gam all users print sendas todrive

Update

Update the SendAs name

gam user <User Email Address> update sendas <User Email Address> name "<Text to Update>"

Delete

gam user <User Email Address> delete sendas <SendAs Email Address>

Sendemail

gam user <Sender Email Address> sendemail recipient <Recipient Email> subject "<Subject Text>" ghtml <File Owner Email> <File ID>

Show Messages

Search

You can use the Gmail search criteria in a GAM command. Create the search in Gmail and then copy 

gam user <User Email Address> show messages query "<Gmail Search>"

<Gmail Search> = for example;-

from:(<Sender Email>) has:attachment -in:chats

from:<Sender Email> to:<To Email> has:attachment \"<Words to search>\"

Save Attachments

Add the saveattachments option to save the attachments. maxtoshow 1 will save the latest attacement.

saveattachments overwrite maxtoshow 1 targetfolder <Path/To/Folder>

Countsonly

Find the number of messages in a query. Needed if you want to delete the messages.

user <User Email Address> show messages query "from:<Email Sender to Delete>" countsonly

TXT File

Sometimes is may be more useful to create a TXT file of the message(s) for ease of sharing

gam user <User Email Address> show messages query "to:<To Email Address>" includespamtrash showbody > <TXT File Name>.txt

Signature

User

Using an HTML file to set a signature for a user. The file is saved in the Cloud.

gam user <User Email Address> signature file </home/your_cloud_instance/Path/to/File/<File Name>.html> html

CSV

https://goo.gl/DXmmsB

https://github.com/taers232c/GAMADV-XTD3/wiki/Tag-Replace#user-attribute-replace-tag-userreplacement-processing

Vacation (Out of Office | Away)

Display Out of Office message

Using the format option is the best way to view the Out of Office message. Change to html if you want to view the code.

gam user <User Email Address> show vacation format

Enable/Disable the Out of Office message

Use on or off depending on whether you want to enable or disable. Note, however, if the End date has passed, no Out of Office message will be shown.

gam user <User Email Address> vacation <On or Off>

Set the Out of Office message. 

By default this will send to all people emailing the user. Use the contactsonly and/or domainonly if you want to specify who receives the message.

gam user <User Email Address> vacation on subject <"Subject Line Text"> message <"Message In The Email Body"> html

If the user has already used an Out of Office message and previously set an end date, that has passed, then changing the Out of Office message will not work. A new end date needs to be set. For safety, use the following to overwrite the start and end date.

start 2000-01-01 end 2999-01-01

Users with Out of Office Messages

The format option will show what the OOO messages will look like, in the command window. Other options are compact & html.

For a user 

gam user <User Email Address> show vacation html

For an OU

gam ou_and_children </Path/To/Ou> show vacation format

gam ou_and_children </Path/To/Ou> print vacation enabledonly todrive

Miscellaneous

Last Interaction Time

gam report users todrive parameters gmail:last_interaction_time

Create Collaborative Inbox

Archive Messages

gam update group <Group Email Address> isarchived true

Change membership and delivery options

gam update group <Group Email Address> update owner|manager allmail|nomail <Owner Email Address>