Projects
A combination of 2 or more commands needed to complete a task
Calendar
Remove Calendar Event
All Calendars
List all the calendars to get the <Calendar Email/ID>.
gam ou_and_children_ns </Path/To/OU> print calendars todrive
Check Name
From the GUI check the settings to get the <Calendar Email/ID>. Although this is not necessary.
gam calendar <Calendar Email/ID> show settings
Show Events
Use this command to find the <Event ID> for the event to delete.
gam calendar <Calendar Email/ID> print events todrive
Delete Event
Test the command without the doit on the end.
gam calendar <Calendar Email/ID> delete event id <Event ID> doit
Contacts
Update Contact Email Address
Discover who has the contact
This will create a Google Sheet of the users with the <Old Email Address> in their contacts. The users will be under the header User.
gam ou_and_children_ns </Path/To/OU> print contacts emailmatchpattern <Old Email Address> todrive
Test
To test, copy the headers and 1 row of data to another sheet. Run this command against this sheet.
gam csv gsheet <User Email Address> <File ID> '<Test Sheet Name>' gam user ~User update contacts emailmatchpattern <existing email address to change> email work <new email address> primary
Change
If the test is sucsessful, change the name of the sheet to the one with all the data and rum the command again.
gam csv gsheet <User Email Address> <File ID> '<Sheet Name>' gam user ~User update contacts emailmatchpattern <existing email address to change> email work <new email address> primary
Drive
Change Ownership To Edit a File
If you need to edit a document you are not an Editor of, you can use the following commands. N.B. only Editors can see file members. The fileinfo option will show you the file owner, which is then needed to add/remove your account.
Find the FileID
If you know the exact name. The anyowner is needed as you are not the owner of the document. Save the FileID.
gam user <User Email Address> show filelist query "title = '<Exact File Name>'" anyowner
View File Info
You need to know an existing Editor to enable you to be added as an Editor. This command will only work if you have view permission to the file.
gam user <Your Email Address> print fileinfo id <FileID>
If you do not have view permission, use this command.
gam show ownership <FileID>
Add you as an Editor
gam user <Document Owner Email Address> add drivefileacl <FileID> user <Your Email Address> role editor
Remove you as an Editor
After making the edits you may want to remove yourself from the Document permissions.
gam user <Document Owner Email Address> delete drivefileacl <FileID> <Your Email Address>
Create a Folder in a Users My Drive and Allow Someone Else to Upload
UseCase:
To cut down internet usage during the day, we want to allow our Help Desk team to upload videos after peak hours (the teaching day) for other staff. They already have access to the local video files stored on our on-premis servers. These commands will allow us to proceed without the end user having to do anything.
Create new folder in Staffs My Drive
Suggest using an underscore so this appears at the top of the Staff Users My Drive
gam user <Staff User Email Address> add drivefile drivefilename "<New Folder Name>" mimetype gfolder
Get the Folder ID for the new folder
gam user <Staff User Email Address> show filelist query "title = '<New Folder Name>'"
for example Folder ID= 1RDXXXXXXXXXXXXXXXXXXXXFST, taken from the webLink.
Add the Help Desk team account to allow them to upload videos
gam user <Staff User Email Address> add drivefileacl <Folder ID> user <Help Desk Email Address> role editor
Remove the Help Desk team account (if needed)
gam user <Staff User Email Address> delete drivefileacl <Folder ID> <Help Desk Email Address>
Find the Owner of a FILE
Open up the Templates page and then the Dev Tools (Inspect).
Search for lh3.google.com/u/0/d/ for the template required and from the URL copy the File ID, for example 1VL67xxxxxxxxxxxxxxxxxxxxxxxxxxsCnEkUaXgRrg.
Then run;-
gam user <Your Email Address> print fileinfo id <File ID>
Or
gam show ownership <File Or Folder ID>
Force Inherit File Permsions
A series of commands to collect the permsions of a file and then use these to pupulate a file in the same folder.
Collect the permissionss of a file in the same folder
gam user <User Email Address> print drivefileacls <File ID with Correct Permissions> oneitemperrow > ./<File Name>.csv
Update New File with same permissions
gam csv <File Name>.csv gam user <User Email Address> update drivefileacl <New File ID> ~permission.emailAddress role ~permission.role
Add a User to another users Drive
Create a CSV of all the files
gam redirect csv filelist.csv user <User Email Address> print filelist fields id,title
Add the New User
gam csv filelist.csv gam user <User Email Address> add drivefileacl ~id user <New User Email Address> role <role>
<role> could be reader, writer, commenter
Convert File to PDF and Upload
This example will convert Jamboard files to PDFs and then upload them to the users MyDrive.
Get a list of Jam files for all users. Substitute for all users as desired
gam config auto_batch_min 1 redirect csv ./JamFiles.csv multiprocess redirect stdout - multiprocess redirect stderr stdout all users print filelist fields id,name,mimetype showmimetype gjam
Download the files into subdirectories by user
gam redirect stdout ./DownloadJamFiles.txt multiprocess redirect stderr stdout csv JamFiles.csv gam user "~Owner" get drivefile "~id" format pdf targetfolder "./JamFiles/#email#" overwrite false
Shared Drive
Copy A Users Folder to a Shared Drive
As Admins can only move Folder from My Drive to Shared Drives (unless the setting is changed), the following can be used to move a users folder to their Shared Drive, with minimal effort from the user.
You will need;-
The user must share their folder with you
The exact name of the shared drive.
The Copy Folder Extension.
Copy the Folder
Using the Copy Folder Extension, create a copy of the shared folder.
Find the Shared Drive ID
gam user <Other Users Email> print teamdriveacls matchname '<Shared Drive Name>' fields id
If they have given you the wrong name or part of the name just run this command to see a list of all their Shared Drives. Then you should be able to work out the Shared Drive ID from the list.
gam user<Other Users Email> print teamdriveacls todrive
Give Yourself Access to the Shared Drive
gam add drivefileacl <Shared Drive ID> user <Your Email Address> role organizer
Move the Folder
Drag and drop the Copy of the Folder.
Remove Yourself from the Shared (Team) Drive
gam user <Your Email Address> delete drivefileacl <Shared Drive ID> <Your Email Address>
Remove Yourself from the Original Shared Folder
gam user <Your Email Address> delete drivefileacl <Drive Folder ID> <Your Email Address>
Change Manager (Organizer) of Shared Drive
Find All Shared Drives
Filter the data to find the Shared Drive ID.
gam print teamdrives todrive
Get the Members
gam print drivefileacls <Team Drive ID> oneitemperrow todrive
Add Member (Manager = Organizer)
gam add drivefileacl <Team Drive ID> user <new organizer email address> role organizer
Remove Original Member
gam user <Shared Drive Manager Email> delete drivefileacl <Shared Drive ID> <User Email To Delete>
Find and Delete File from My Drive and Shared Drives
This looks for files by title.
Finding the files
gam user <User Email Address> print filelist query "title='<File Title>'" id title > <CSV File Name>.csv
Or, saving the results to your My Drive. You will then need to manipulate the results into the CSV file before using to delete the files
gam user <User Email Address> print filelist query "title='<File Title>'" id title todrive
Delete from My Drive
gam csv <CSV File Name>.csv gam user <User Email Address> delete drivefile ~id
Delete from Shared Drives
gam user <User Email Address> print filelist corpora onlyteamdrives query "title='<File Title>'" id title > <CSV File Name>.csv
Copy Team Drive to another Shared Drive
Find a Team Drive ID
Create a sheet of the Team Drives a user belongs to
gam user <Leaving User Email Address> print teamdrives todrive
Or, to use a CSV for adding access permission
gam user <Leaving User Email Address> print teamdrives > <CSV File Name>.csv
Add Admin User to the Team Drive
Add Admin User to the Source Team Drive. I do this this way, rather than running the command as the leaving user, so that they do not get access to the new Shared Drive until it is all finished.
gam add drivefileacl <Team Drive ID> user <User Email Address> role organizer
Or, using the CSV from above
gam csv <CSV File Name>.csv gam add drivefileacl ~id user <User Email Address> role organizer
Check the number of files in the Shared Drive
This gives an indication of how long the copy will take and can be used for checking after the copy. (NB filecounts only works with top level Team Drives)
gam user <User Email Address> print filecounts select teamdriveid <Shared Drive ID>
Or, if you just want the number
gam user <User Email Address> show filecounts select teamdriveid <Shared Drive ID>
Copy the Team Drive
gam user <User Email Address> copy drivefile <Source Shared Drive ID> recursive copytopfolderpermissions false copyfilepermissions false copysubfolderpermissions false teamdriveparentid <Destination Shared Drive ID> newfilename "<New Folder Name>"
Check the files are all copied
Check the files in the copied folder, using;-
gam user <User Email Address> print filelist select <Copied Folder ID> countsonly fields mimetype
Or, Using VLOOKUP to compare the file list, to make sure all copied, of
gam user <User Email Address> print filelist select teamdriveid <Shared Drive ID> todrive tdtitle "<Sheet Name>"
AND
gam user <User Email Address> print filelist select teamdrivefilename "<Folder Name>" todrive tdtitle "<Sheet Name>"
Remove Admin User from Team Drive Permissions
Remove access for a single Shared Drive, where the <User Email Address> is your/Admin email address.
gam user <User Email Address> delete <Shared Drive ID> <User Email Address>
Or, remove using the CSV file created at the start of the process
gam csv <CSV File Name>.csv gam user <User Email Address> delete drivefileacl ~id <User Email Address>
Move a File or Folder to Shared Drive
Find the Owner of the File or Folder
gam show ownership <File Or Folder ID>
Add a New User
This user will later become the Owner
gam user <Owner Email Address> add drivefileacl id <File Or Folder ID> user <New User Email> role writer
Claim Ownership
gam user <New User Email> claim ownership id <File Or Folder ID> keepuser
Move to Shared Drive
gam user <New User Email> move drivefile <File Or Folder ID> teamdriveparentid <Team Drive ID>
Groups
Set up Web Forums
These commands can be used to create multiple groups and set them as Web Forums (although this needs to be done via Group Settings GUI). I am sure with more time the names/emails could be combined into a single CSV and run more efficiently.
Set up the Groups
The <All Groups>.csv CSV contains a list of all the new email addresses for the groups and their names.
gam csv <All Groups>.csv gam create group ~email
Configure the Groups
Once the Groups have been created, you will need to access the Settings UI for each and select Web forum from Information > Advanced > Select a group type. This command then hides the group from the directory, so can only be found from the Groups homepage, makes sure the messages are archived and only allows Group Members to post.
gam csv <All Groups>.csv gam update group ~email name ~name includeinglobaladdresslist false isarchived yes whocanpostmessage all_members_can_post
Add the Managers
gam csv <All Groups>.csv gam update group ~email add manager <Manager 1 Email Address>,<Manager 2 Email Address>
Add the members
The <Group Memebers>.csv CSV has a list of all the email addresses for the members of the Group.
gam csv <Group Memebers>.csv gam update group <Group Email Address> add member user ~email
Set so that the members do not receive email
All viewing/posting will be via the Groups homepage.
gam csv <Group Memebers>.csv gam update group <Group Email Address> update member nomail ~email
Print the Group info
This will create a gSheet for all the groups so the settings can be checked.
gam print groups domain <Domain> allfields todrive
But to see info for a single group
gam print groups select <Group Email Address> todrive
People
Update Directory Sharing Status
Find the users who have Directory sharing disabled. This is by OU, so you can run on top level OU (orgUnitPath='/') or any Sub-OU and only shows current (Not Suspended) users.
gam print users fields name,gal query "orgUnitPath='/<OU Name>' isSuspended=False" todrive tdtitle "Directory Sharing"
Use Google Sheets to remove the users who have includeInGlobalAddressList = TRUE. These users already have Directory sharing enabled. Save users as a CSV file (example gal.csv). Upload to Google Cloud.
Update users by running
gam csv gal.csv gam update user ~primaryEmail gal true
Or reset/update based on field from the CSV file.
gam csv gal.csv gam update user ~primaryEmail gal ~includeInGlobalAddressList
Delegate Accounts (Exams, Visitors etc)
We have a set of accounts that are used for various scenarios, such as for exams, visiting groups of staff or students, interview candidates etc. We call these delegate accounts and are not related to the 'delegated email accounts' in Gmail.
This is the process I have used to reset these accounts after they have been used so that all the emails and Drive Docs are removed.
The delegates.csv contains the email addresses for all the accounts you need to clear.
Remove the Email
This command assumes that there are less than 100 messages to delete. You may need to increase this if more messages are in the mailbox. You can replace max_to_delete 100 with before:yyyy/mm/dd but this will also delete the 3 emails from Google that appear when the account is created.
gam csv delegates.csv gam user ~email delete messages query deliveredto:~~email~~ max_to_delete 100
Transfer Drive files
The easiest way is to use the following command. This will collect all the files and create a folder in the <My Email Address> account. The folder will be called "<delegate email address> old files". Inside this folder there will also be a folder called "<delegate email address> orphaned files" which will contain files shared with that account.
gam csv delegates.csv user ~email transfer drive <My Email Address>
Aggregate Emails RECEIVED and Sent for a Group of users
These are the commands to first print out the members of a Group and then work out the combined daily aggregate data for each day in the range.
Get the members of a group
The _ns (not suspended) at the end of the group_ns will ensure that only active members will be listed.
gam print group-members group_ns <Group Email Address> todrive
Take the output Google Sheet and copy-paste transposed, the email list, so you have a row of email addresses. Save as a CSV and use this row of users to replace testuser1,testuser2,testuser3 in the command below.
Get the Data
gam report users select users testuser1,testuser2,testuser3 fields gmail:num_emails_received,gmail:num_emails_sent range <Start Date> <End Date> aggregatebydate todrive
Exam Results | Send to Users
This set of commands can be used to send out individual exam results to a set up students. The results are PDF files stored in a users My Drive.
Get the folder id
The output of this command is the <FolderID> for the folder where the PDFs are stored.
gam user <Folder Owner Email> show filelist query "title ='<Folder Name>'" id title
Get the file IDs and titles from the folder
gam user <Folder Owner Email> show filelist query "'<FolderID>' in parents" id title todrive
Add Student emails to Google Sheet created above
Next, add the student emails into the gSheet that has been created. Call the field 'email'.
Create CSV
Download as CSV and rename examresults.csv. Or Alternatively share using this gSheet direct.
Share using CSV
This command will send the Drive File Share notification email to the students listed in the Google Sheet.
gam csv examresults.csv gam user <Folder Owner Email> add drivefileacl ~id user ~email role reader sendemail emailmessage '<Your Message>'
Share using gSheet
Alternatively, share the files using the gSheet created with all the student & PDF data.
gam csv gsheet <gSheet Owner Email> <gSheet File ID> '<gSheet Name>' gam user <Folder Owner Email> add drivefileacl ~id user ~email role reader sendemail emailmessage '<Your Message>'