Hoping I can get some assistance with something I am stuck on.
Periodically I need to pull a report of inactive users so that I can then disable or delete accounts to free up Office 365 licenses. I am using as follows…
Primarysmtpaddress comes back blank. And that’s using Export-CSV or Format-Table.
I know it’s an intensive search and I also know Get-MailboxStatistics isn’t very efficient. But my question is how can I reliably get this sort of report from O365 using Powershell?
I cannot test at the moment, but if I’m not wrong Get-MailboxStatistics does not output a property PrimarySMTPAddress. That should be a property of the output of Get-Mailbox. Therefore you cannot simply use Select-Object in this case …
Something like this should work actually
Thanks. You’re right about that. I’m a newbie still and didn’t realize what I was passing to the pipeline. I did a gm on Get-MailboxStatistics to confirm.
I’ll try what you posted, should work for me. Thanks so much!
Periodically I need to pull a report of inactive users so that I can then disable or delete accounts to free up Office 365 licenses.
… why reinvent the wheel, when MS provide direct guidance on how to handle this.
There is a specific cmdlet for finding user state, depending on what OS/PS version you are running, the help files show:
# get function / cmdlet details
(Get-Command -Name Search-ADAccount).Parameters.Keys | Sort
Get-help -Name Search-ADAccount -Full
Get-help -Name Search-ADAccount -Online
Get-help -Name Search-ADAccount -Examples
Function Get-HelpExamples
{
[CmdletBinding()]
[Alias('ghe')]
Param
(
[string]$CmdletName = (
Get-Command -Name '*' |
Out-GridView -PassThru -Title 'Select a cmdlet to see examples'
)
)
If ((Get-Help -Name $CmdletName).Examples)
{
(((Get-Help -Name $CmdletName).Examples |
Out-String -Stream) -match '.*\\>|C:\\PS>') -replace '.*\\>|C:\\PS>' |
Out-GridView -Title 'Select a sample to use' -PassThru
}
Else {Write-Warning -Message "The were no help examples discovered"}
}
ghe -CmdletName Search-ADAccount
# Results
Search-ADAccount -AccountDisabled | FT Name,ObjectClass -A
Search-ADAccount -AccountDisabled -UsersOnly | FT Name,ObjectClass -A
Search-ADAccount -AccountExpired | FT Name,ObjectClass -A
Search-ADAccount -AccountExpiring -TimeSpan 6.00:00:00 | FT Name,ObjectClass -A
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 | FT Name,ObjectClass -A
Search-ADAccount -PasswordExpired | FT Name,ObjectClass -A
Search-ADAccount -PasswordNeverExpires | FT Name,ObjectClass -A
Search-ADAccount -LockedOut | FT Name,ObjectClass -A
Search-ADAccount -AccountDisabled -ComputersOnly | FT Name,ObjectClass -A
Search-ADAccount -AccountExpiring -DateTime "3/18/2009" | FT Name,ObjectClass -A
Search-AdAccount -AccountDisabled -SearchBase "DC=AppNC" -Server "FABRIKAM-SRV1:60000"
Remove O365 Licenses from Disabled Active Directory Users
This Script will remove Office 365 licenses from synchronized users that have been blocked within Office 365 because they are disabled in Active Directory. Azure AD Connect doesn’t remove an O365 license when a user’s Active Directory account gets disabled.
Remove O365 Licenses from Disabled Active Directory Users
This Script will remove Office 365 licenses from synchronized users that have been blocked within Office 365 because they are disabled in Active Directory. Azure AD Connect doesn’t remove an O365 license when a user’s Active Directory account gets disabled.