Office 365 powershell

Ben Smith
Ben Smith used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dope ADMSr. Systems Engineer

Commented:
Correction: Please disregard the above, reviewing the select part....will update you shortly
Dope ADMSr. Systems Engineer

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

Jose Gabriel Ortega CastroCEO Faru Bonon IT /Top Rated Freelancer on Upwork / Photographer
Awarded 2018
Distinguished Expert 2018

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

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
{ $_.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.
Most Valuable Expert 2015
Distinguished Expert 2018
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
PberSolutions Architect

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial