I am using the below code in PowerShell to create a CSV file from Azure AD which lists our North America users and some of their account properties. The current code works great. However, I am trying to add an additional column/property to the report to pull the SAM Account Name
param(
[string] $path = "C:TempADUsersNA-$(Get-Date -format 'MM-dd-yyyy').csv"
)
Connect-AzureAD
$userData = @()
foreach ($azuser in Get-AzureADUser -All $true -Filter "accountEnabled eq true" | Where-Object { $_.Country -eq "United States" -or $_.Country -eq "Canada" }) {
$manager = Get-AzureADUserManager -ObjectId $azuser.ObjectId -ErrorAction SilentlyContinue
$managerEmail = if ($manager) { $manager.UserPrincipalName } else { "" }
$userData += [pscustomobject]@{
"Employee ID" = $azuser.ExtensionProperty["employeeId"]
"First Name" = $azuser.givenName
"Last Name" = $azuser.surname
"Work Email" = $azuser.UserPrincipalName
"Job Title" = $azuser.JobTitle
"Company" = $azuser.CompanyName
"Manager Email" = $managerEmail
"City" = $azuser.City
"Country" = $azuser.Country
}
}
$userData | Export-Csv -Path $path -NoTypeInformation
So far, under the “Country” row I tried using:
"SAM Name" = $azuser.SamAccountName
"SAM Name" = $azuser.ExtentionProperty["SamAccountName"]
"SAM Name" = $azuser.ObjectID["SamAccountName"]
None of the above solutions have been able to successfully pull SAM Account name to the CSV file.