Microsoft 365 features that help users manage their subscriptions, account settings, and billing information.
Dear Lorenzo J225,
Good day! Thank you for posting to Microsoft Community. We are happy to help you.
Based on your description, I understand that you have a query "Extract users and groups using PowerShell".
Could you please explain clearly so we may further help you? Are you referring to extracting Office 365 users and groups using PowerShell? if so, have you tried using the following command:
$Group = Get-AzureADGroup -SearchString "<GroupName>"
Get-AzureADGroupMember -ObjectId $Group.ObjectId -All $true | Select DisplayName, UserPrincipalName |
Export-CSV "C:\GroupMembers.csv" -NoTypeInformation -Encoding UTF8
The command you provided, and the article seems to work only in the Active directory, which is outside our support boundary, however I have done some research; I found this script that can help you accomplish this work.
Get-ADGroup -Filter * | ForEach-Object {Get-ADGroupMember $_ | Select @{Name="GroupName";Expression={$_.Name}},@{Name="UserName";Expression={$_.SamAccountName}}} | Export-Csv -Path "C:\UsersAndGroups.csv" -NoTypeInformation
This command will retrieve all the groups in your Active Directory and then for each group, it will retrieve the members and select the group name and member name. Finally, it will export the data to a CSV file located at "C:\UsersAndGroups.csv".
Make sure you have the Active Directory module installed on your computer before running this command. You can install it by running the following command: Install-WindowsFeature RSAT-AD-PowerShell. Please refer to Getting users and group - Microsoft Community Hub.
If the issue persists; Since the issue is related to On-prem and the issue with Active directory and we here are mainly dedicated to the Online environment, to provide better assistance, we'd like to suggest you post your concern in Microsoft Q&A forum. There would be the engineers have rich experience on on-prem to further help you.
We do appreciate for your understanding and patience.
Regards,
De Paul | Microsoft Community Moderator