-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-AzureADUsersCSVExport.ps1
43 lines (39 loc) · 2.03 KB
/
Get-AzureADUsersCSVExport.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<#
.SYNOPSIS
Export all Azure AD users in a CSV file in current folder
.DESCRIPTION
Azure AD PowerShell Module required,
.NOTES
File Name : Get-AzureADUsersCSVExport.ps1
Author : Sébastien Paulet (SPT Conseil)
Prerequisite : PowerShell V5.1 and Azure AD PowerShell Module
Source/Inspiration : https://docs.microsoft.com/fr-fr/office365/enterprise/powershell/view-account-license-and-service-details-with-office-365-powershell
.PARAMETER ExportName
Used in CSV export name (use company/tenant name)
.EXAMPLE
.\Get-AzureADUsersCSVExport.ps1 -ExportName "Contoso"
#>
param(
[Parameter(Mandatory = $true)]
[String]$ExportName
)
$isConnectedBefore = $false
try {
Get-AzureADSubscribedSku | Out-Null
Write-Verbose 'Open Azure AD connexion found'
$isConnectedBefore = $true
} catch {}
if (-not $isConnectedBefore) {
Write-Verbose 'Connecting to Azure AD'
Connect-AzureAD
}
$dateFileString = Get-Date -Format "FileDateTimeUniversal"
mkdir -Force "$pwd\$ExportName\" | Out-Null
Write-Verbose 'Request all users'
$allUsers = Get-AzureADUser -All $true | Select ObjectType, Mail, UserPrincipalName, UserType, AccountEnabled, AgeGroup, City, CompanyName, ConsentProvidedForMinor, Country, CreationType, Department, DirSyncEnabled, DisplayName, FacsimileTelephoneNumber, GivenName, IsCompromised, ImmutableId, JobTitle, LastDirSyncTime, LegalAgeGroupClassification, MailNickName, Mobile, OnPremisesSecurityIdentifier, PasswordPolicies, PasswordProfile, PhysicalDeliveryOfficeName, PostalCode, PreferredLanguage, ProxyAddresses[0], RefreshTokensValidFromDateTime, ShowInAddressList, SipProxyAddress, State, StreetAddress, Surname, TelephoneNumber, UsageLocation, UserState, UserStateChangedOn
Write-Verbose 'Export to CSV'
$allUsers | Export-Csv -Path "$pwd\$ExportName\UsersExport-$ExportName-$dateFileString.csv" -Delimiter ';' -Encoding UTF8 -NoTypeInformation
if (-not $isConnectedBefore) {
Write-Verbose 'Disconnecting from Azure AD'
Disconnect-AzureAD
}