Office 365 powershell

Hi,

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

Ben SmithAsked:
Who is Participating?
 
Vasil Michev (MVP)Commented:
You know there's a built-in report for that, right? O365 Admin portal -> Reports -> Usage -> Email activity or directly via https://portal.office.com/adminportal/home#/reportsUsage/EmailActivity
0
 
Dope ADMSr. Systems EngineerCommented:
Correction: Please disregard the above, reviewing the select part....will update you shortly
0
 
Dope ADMSr. Systems EngineerCommented:
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

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
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 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

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 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

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
{ $_.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.
1
 
PberSolutions ArchitectCommented:
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.

Pber
Experts-Exchange Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.