Powershell script to check disables user in AD having license in MS365 portal

$0365user = Get-MsolUser -All | where {$_.isLicensed -eq $true } | Format-Table displayname 
$filter = {Enabled -eq $false}
$aduser = Get-ADUser -Filter $filter | Format-Table name
foreach ($o365user in $aduser)
{Write-Output $o365user | Format-Table displayname}

I am new to powershell. I am trying to query user who are disabled/de-active in AD but have license enabled in MS365 portal using above script but the data i am getting in wrong.

Can someone help me?

Thanks,

I don’t have an 365 environment to test, but this should display a list of AD users that exists in the 365 user list.

$O365user = (Get-MsolUser -All | Where-Object {$_.isLicensed -eq $true }).DisplayName
$aduser = (Get-ADUser -Filter {Enabled -eq $false}).DisplayName

$list = foreach ($a in $aduser){
    If ($O365user -contains $a){$a}
}
$list