Avatar of Ben Smith
Ben Smith
 asked on

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

PowershellMicrosoft 365

Avatar of undefined
Last Comment
Pber

8/22/2022 - Mon
Dope ADM

Correction: Please disregard the above, reviewing the select part....will update you shortly
Dope ADM

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 Castro

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Qlemo

{ $_.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.
ASKER CERTIFIED SOLUTION
Vasil Michev (MVP)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Pber

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