Hello @Prince Chauhan,
Thank you for posting your query on Microsoft Q&A.
I understand that you want to export few user information like userPrincipalName, displayName, surname, mail, userType, accountEnabled, creationType, LastSigninDate and On-Premises sync enabled through PowerShell script.
Please follow the step by step process as mentioned below.
1.Open PowerShell as an Administrator.
2.Install Microsoft Graph Module. Run the following command to install the Microsoft.Graph module.
Install-Module Microsoft.Graph
3.Import Microsoft Graph Module by running the below PowerShell command.
Import-Module Microsoft.Graph
4.Authenticate with Microsoft Graph
You need to authenticate your session with the appropriate permissions. The command below will prompt you to log in and grant the necessary permissions (you’ll need Directory.Read.All and AuditLog.Read.All scopes):
Connect-MgGraph -Scopes Directory.Read.All,AuditLog.Read.All
A sign-in window will appear where you need to enter your Microsoft Entra ID credentials. Please ensure you have the required permissions to access this data.
5.Run the Command to Export User Data
The following command retrieves user details (like userPrincipalName, displayName, surname, mail, userType, accountEnabled, creationType, LastSigninDate and On-Premises sync enabled) and exports it to a CSV file located at C:\EntraUserDetails.csv
$Users = Get-MgUser -All -Property 'UserPrincipalName','DisplayName','Surname','Mail','CreationType','UserType','OnPremisesSyncEnabled','SignInActivity'
$FilteredUsers = $Users | Select-Object `
@{Name='UserPrincipalName'; Expression={$_.UserPrincipalName}},
@{Name='DisplayName'; Expression={$_.DisplayName}},
@{Name='Surname'; Expression={$_.Surname}},
@{Name='Email'; Expression={$_.Mail}},
@{Name='CreationType'; Expression={$_.CreationType}},
@{Name='UserType'; Expression={$_.UserType}},
@{Name='LastSignInDate'; Expression={$_.SignInActivity.LastSignInDateTime}},
@{Name='OnPremisesSyncEnabled'; Expression={$_.OnPremisesSyncEnabled}}
$FilteredUsers | Export-Csv -Path "C:\EntraUserDetails.csv" -NoTypeInformation -Encoding UTF8
6.Check the Output
After the script runs, the CSV file will be saved at C:\EntraUserDetails.csv. You can open this file in Excel or any other CSV viewer to check the exported data.
I hope this above information provided is helpful. Please feel free to reach out if you have any further questions.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".