PowerShell – Delete old IIS logs

Just a quick script that may come in useful to you. The below PowerShell script will delete IIS Log files older than 7 days that are in the “C:\inetpub\logs\LogFiles\w3svc1” directory. You can alter the script to how many days old the log files can be and also your IIS log location.

get-childitem -Path C:inetpublogsLogFilesw3svc1 -recurse | where-object {$_.lastwritetime -lt (get-date).addDays(-7)} | Foreach-Object { del $_.FullName }


PowerShell – Search the Public Folder

If you want search in the public folder database for an folder you can use the scripts below.

If you know the exact name of the folder:

Get-PublicFolder -Identity "" -Recurse | where {$_.Name -eq "FOLDERNAME"}

If you don’t know the exact name:

Get-PublicFolder -Identity "" -Recurse | where {$_.Name -like "FOLDER*"}


How to Delete the First Database and Move the System Mailboxes

When you try to delete the Default Mailbox Database(created with 1st installation of the Mailbox Role), you receive the error below:

“This mailbox database contains one or more mailboxes or arbitration mailboxes. Before you can remove this mailbox database, you must disable, move or remove user mailboxes and move arbitration mailboxes.”

The First database contains the SystemMailboxes which are the Arbitration mailbox(es).

We have to move these system mailboxes to another database before of remove the Default Database.

1. Find the Arbitration Mailbox using the Exchange MAnagement Shell (EMS):

Get-Mailbox -Arbitration | Where {$_.Name -like "SystemMailbox*" } | ft –wrap, name, alias, database

2. Now create a new move request in order to move the system mailboxes to another mailbox database:

New-MoveRequest -Identity "SystemMailbox{1f05a927-32d1-4e19-8ea5-67eba859f541-yourGUID}" -TargetDatabase "dbxxxx"

3. Verify whether the move request completed:


4. Now remove the move request

Remove-MoveRequest -Identity "the name of the request like Microsoft Exchange Approval Assistant"

PowerShell – Disconnected mailboxes are not yet marked as disconnected

Use the Clean-MailboxDatabase cmdlet to scan the Active Directory directory service for disconnected mailboxes that are not yet marked as disconnected in the Microsoft Exchange store and update the status of those mailboxes in the Exchange store.
This cmdlet is not able to update the Exchange store unless the Microsoft Exchange Information Store service is running and the database is mounted.

Run from exchange management shell:

$mailboxDatabase = Get-MailboxDatabase

foreach ($cont in $mailboxDatabase){
	clean-MailboxDatabase -Identity $cont.Name

From remote or normal Powershell window:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
	$mailboxDatabase = Get-MailboxDatabase	
	foreach ($cont in $mailboxDatabase){
		clean-MailboxDatabase -Identity $cont.Name
Remove-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010


PowerShell – Check if email address exists

You can use -ANR to search using Ambiguous Name Resolution (so you can type their first, last, username, etc), but while ANR will match a user’s primary email address, it will not match their other SMTP aliases. ANR can return multiple matches (so ensure your code handles this case).

For email address:

get-mailbox -Anr user@domain.com

For public folder email address:

Get-MailPublicFolder -Anr user@domain.com

– Pouyan