Link to home
Create AccountLog in
Avatar of Ben Smith
Ben Smith

asked on

Office 365 powershell


I am looking to export a list of enabled users in Office365 to csv who haven't sent an email in over 90 days, I started working on a script but keep getting errors, any suggestion?

$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

Open in new window

Avatar of Dope ADM
Dope ADM
Flag of United States of America image

Correction: Please disregard the above, reviewing the select part....will update you shortly
Try this and let me know if it works:

$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

Open in new window

Avatar of J0rtIT
You are piping 2 differents Cmdlets from 2 different "sources".

O365 contains the "MsolService" conenction that is what you see in the portal of o365 administration.
But the Get-mailbox is only available in the "Exchange Online" Environment.

So that way will never work bud.
How should you go then?
1st Solve the Exchange Online to find out those who haven't logged in into the last 90 days,
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -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

Open in new window

This way on the var "Inactive90"  (that only brings DisplayName as an important field.

The end result will be like:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

$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

$Result| ConvertTo-Csv -NoTypeInformation file.csv

Open in new window

{ $_.LastLogonTime -lt (get-date).AddDays(-90) } isn't the same as "haven't sent an email in over 90 days". The user might have read emails, and therefor needed to log in.
Avatar of Vasil Michev (MVP)
Vasil Michev (MVP)
Flag of Bulgaria image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Vasil Michev (MVP) (https:#a42429608)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer