Avatar of Anthony K O365
Anthony K O365
Flag for United States of America asked on

Find Disconnected Exchange 2016 mailboxes

When I run this cmdlet, I get the expected output. 

Get-mailboxDatabase | Get-MailboxStatistics    -filter {displayname -like "*Hanna*"}  | ? { $_.DisconnectDate -ne $null } | FT DisplayName,MailboxGuid,Database,DisconnectDate


However, when I run this as a PowerShell script, it returns nothing. What am I doing wrong? Please assist. Thanks!!


Add-PSSnapin *Exchange*

$user = Read-Host "Add displayName here..."

Get-mailboxDatabase | Get-MailboxStatistics -filter {displayname -like "*$user*"}  | ? { $_.DisconnectDate -ne $null } | FT DisplayName,MailboxGuid,Database,DisconnectDate
ExchangePowershell

Avatar of undefined
Last Comment
Anthony K O365

8/22/2022 - Mon
Michael Pfister

Can't test here but you can give

$user = Read-Host "Add displayName here..."
Get-mailboxDatabase | Get-MailboxStatistics | ? {$_.displayname -like "*$user*"}  | ? { $_.DisconnectDate -ne $null } | FT DisplayName,MailboxGuid,Database,DisconnectDate

Open in new window


a try.

Or
$user = Read-Host "Add displayName here..."
$user = "*"+$user+"*"
Get-mailboxDatabase | Get-MailboxStatistics -filter {displayname -like  $user}  | ? { $_.DisconnectDate -ne $null } | FT DisplayName,MailboxGuid,Database,DisconnectDate

Open in new window

Saif Shaikh

This should work.

Get-MailboxStatistics -Server ExMBX01|
where { $_.DisconnectDate -ne $null }
| select DisplayName,MailboxGuid,Database,DisconnectDate

Anthony K O365

ASKER
Unfortunately neither one of these worked. it return nothing. Sorry
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
oBdA

You can't use variables in the filter if you pass it as a scriptblock.
Add-PSSnapin *Exchange*

$user = Read-Host "Add displayName here..."

Get-MailboxDatabase | Get-MailboxStatistics -Filter "DisplayName -like '*$($user)*'" | Where-Object {$_.DisconnectDate} | Format-Table DisplayName, MailboxGuid, Database, DisconnectDate

Open in new window


Get-MailboxStatistics
https://docs.microsoft.com/en-us/powershell/module/exchange/get-mailboxstatistics?view=exchange-ps

-Filter
This parameter is available only in on-premises Exchange.
[...]
* Enclose the whole OPath filter in double quotation marks " ". If the filter contains system values (for example, $true, $false, or $null), use single quotation marks ' ' instead. Although this parameter is a string (not a system block), you can also use braces { }, but only if the filter doesn't contain variables.
[...]
* Value is the property value to search for. Enclose text values and variables in single quotation marks ('Value' or '$Variable'). If a variable value contains single quotation marks, you need to identify (escape) the single quotation marks to expand the variable correctly. For example, instead of '$User', use '$($User -Replace "'","''")'. Don't enclose integers or system values (for example, 500, $true, $false, or $null).
Anthony K O365

ASKER
Can you clarify your script? I"m getting the following error:


Invalid filter syntax. For a description of the filter parameter syntax see the command help.
""displayName -like '*$($user)*'"" at position 1.
    + CategoryInfo          : NotSpecified: (:) [Get-MailboxStatistics], ParsingSyntaxException
    + FullyQualifiedErrorId : [Server=*****,RequestId=d7a87947-d781-4e4e-ae0aa1be76eb9e44,TimeStamp=1/25/2022 1:
   16:56 PM] [FailureCategory=Cmdlet-ParsingSyntaxException] CAED2117,Microsoft.Exchange.Management.MapiTasks.GetMailboxStatistics
ASKER CERTIFIED SOLUTION
oBdA

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.
Anthony K O365

ASKER
Excellent!! That solve my issue. 
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.