List Files and Access Permissions Using Apps Script

If you’re looking to manage your Google Drive files and understand who has access to them, Google Apps Script can be a powerful tool. Let’s see how to create a script that lists all your files in a specific Google Drive folder and the users who have access to these files. This is particularly useful for managing shared documents and ensuring the right people have the correct access.

We will write a script that iterates through all files in a specified folder and logs the names of these files and the users who have access.

Here’s an example of what this script will look like:

function listFilesAndPermissions() {
  var folderId = 'YOUR_FOLDER_ID_HERE'; // Replace with your folder ID
  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFiles();
  
  while (files.hasNext()) {
    var file = files.next();
    var fileName = file.getName();
    var sharingAccess = file.getSharingAccess();
    var sharingPermission = file.getSharingPermission();
    var editors = file.getEditors();
    var viewers = file.getViewers();

    Logger.log('File Name: ' + fileName);
    Logger.log('Access: ' + sharingAccess);
    Logger.log('Permission: ' + sharingPermission);

    editors.forEach(function(editor) {
      Logger.log('Editor: ' + editor.getEmail());
    });

    viewers.forEach(function(viewer) {
      Logger.log('Viewer: ' + viewer.getEmail());
    });

    Logger.log('-------------------');
  }
}
  • Replace 'YOUR_FOLDER_ID_HERE' with the ID of the Google Drive folder you want to inspect.
  • Save your script and run the function listFilesAndPermissions.
  • Google Apps Script will ask for permission to access your Google Drive. Grant these permissions.
  • Once the script has run, view the logs by clicking on View > Logs the script editor.
  • Here you will see a list of files, along with the users who have edit or view permissions.

The script will output:

  • The name of each file in the specified folder.
  • The access level (e.g., anyone with the link, private).
  • The permission level (e.g., edit, view).
  • A list of users with edit permissions.
  • A list of users with view permissions.

Feel free to modify this script to suit your specific needs, such as extending it to include more detailed file information or adding additional functionality to modify permissions directly from the script.

If you are looking for a more Google Workspace Addon to help you audit and scan your Google Drive files, Here is a Drive Permissions Scanner – by 8apps.co