CSOM library have SPGroups object to get all the groups within sites and site collection. The PowerShell script in this location will export all the groups and users within each group as a .csv file format.
Below is the sample output from the PowerShell window.
Below is the sample CSV format of the exported Group and Users.