$UserStats = Get-mailbox -resultsize unlimited | Get-MailboxStatistics | where {$_.LastLogonTime -lt ((get-date).AddDays(-90))} | Get-MsolUser -EnabledFilter EnabledOnly |
Select userprincipalname,Licenses,lastlogontime > c:\ps\export.txt
$CollectUsers = Get-mailbox -resultsize unlimited -RecipientTypeDetails UserMailbox,SharedMailbox |Get-MailboxStatistics |where {$_.LastLogonTime -lt ((get-date).AddDays(-90))}
$UserStats = @()
foreach($user in $CollectUsers){
$obj = "" | select userprincipalname,Licenses,LastLogonTime
$x = Get-MsolUser -UserPrincipalName (Get-Mailbox $user.MailboxGuid.Guid).UserPrincipalName | ? {$_.isLicensed -and !($_.BlockCredential)}
$obj.LastLogonTime = $user.LastLogonTime
$obj.userprincipalname = $x.userprincipalname
$obj.Licenses = $x.Licenses
$UserStats += $obj
}
$UserStats | Out-File c:\ps\export.txt
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$Inactive90=Get-mailbox -resultsize unlimited |select * | Get-MailboxStatistics | where {$_.LastLogonTime -lt ((get-date).AddDays(-90))} | select DisplayName
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$Result=@()
$Inactive90= Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | where {$_.LastLogonTime -lt ((get-date).AddDays(-90))}
Connect-MsolService -credential $UserCredential
foreach($user in $Inactive90){
$search=Get-MsolUser -EnabledFilter EnabledOnly | where{ $_.DisplayName -eq $user.DisplayName} | select userprincipalname,Licenses,lastlogontime
if(!($search)){
$Result+=$search
}
}
$Result| ConvertTo-Csv -NoTypeInformation file.csv