Powered by Ed Buford and Coffee

Category: Powershell

Disabling the URL Preview in Office 365 email using PowerShell

You can quickly disable the Link Preview in Office 365 for a single user setting the LinkPreviewEnabled attribute to $false when you type: 

Get-MailboxMessageConfiguration –identity ebuford –LinkPreviewEnabled $false

You can also do this for All your users if you like by getting all the mailboxes and setting them at once:

get-mailbox | Set-MailboxMessageConfiguration -LinkPreviewEnabled $false

Clean that Database!

Clean Database command missing in Exchange 2013

Sometimes you need to disable a mailbox you need to be able to see it as a disconnected mailbox right away. In the Past you’ve been able to use the Clean-Mailboxdatabase command.  I would normally tend to run the clean command against all databases since the command doesn’t draw much in the way of resources. So for me the command would look like this:    Get-Mailboxdatabase | Clean-Mailboxdatabase

But as with everything the times are constantly changing and so are the commandlets that go with them. Which brings me to Exchange 2013 where the Clean-Mailboxdatabase command no longer works. Now the command is Update-StoreMailboxState and sadly it’s not a simple command to clean all the databases – in fact to run the command you need to know the database and mailbox GUID. Now this is going far… but I digress…

Anyway if you know the database you can attack it like this:

Get-MailboxStatistics –Database DB01 | ForEach { Update-StoreMailboxState -Database $_.Database -Identity $_.MailboxGuid -Confirm:$false }


GetMailboxStatistics Database MDB02 |ForEach { UpdateStoreMailboxState Database $_.Database Identity $_.MailboxGuid Confirm:$false }


Finding AD Groups with PowerShell

How to List AD Groups by type using PowerShell

The AD group type is a bit of a mystery to me. I’m not sure why Microsoft has chosen to make thing they way they have and I have to keep reminding myself they have been building Active Directory a lot longer than they have been building PowerShell.
Today one of my team asked me to see if I could pull Domain Local groups out of AD using PowerShell. I was sure this was going to be as easy as it sounds. Turns out it isn’t straight forward.

Since there isn’t a Get-ADGroupType PowerShell command I went looking at the Attributes and here’s what I found:


Even more confusing when you Open that Attribute you get something even more interesting:



So I started searching around MSDN and I came up with this chart:

Group Type                                                 Value
Global distribution group                        2
Domain local distribution group           4
Universal distribution group                  8
Global security group                               -2147483646
Domain local security group                  -2147483644
Universal security group                         -2147483640

Now that I have the value I’m looking for I can pull it out of AD:
In order to do that I need to log into Domain Controller or a Domain computer with RSAT loaded.  Then I can import the ActiveDirectory module:

Import-module ActiveDirectory

Then I can get the Group Type by using the command below

Get-ADGroup -Filter * -Properties GroupType | where {$_.GroupType -eq “-2147483644”} | FL name

If I want to change the Group Type that I’m searching for then I just change the number from the list above make sure to include the Negative on the ones that have it listed.

[sourcecode language='powershell' ]
Get-ADGroup -Filter * -Properties GroupType | where {$_.GroupType -eq "-2147483644"} | FL name


Moving FSMO Roles with PowerShell

If you’ve ever had to move FSMO roles in Active Directory to another server you know it’s not as straight forward as it could should be (At least until I found PowerShell).

Open the Active Directory Module for Windows PowerShell or open PoweShell on a machine with RSAT installed and Import-Module ActiveDirectory


Part of the FSMO roles are Domain and part are Forest so you’ll need to use 2 commands to get the roles.
To get the Domain role holders:

Get-ADDomain | select PDCEmulator,RIDMaster,InfrastructureMaster

To get the Forest role holders:

Get-ADForest | select SchemaMaster,DomainNamingMaster

Moving roles only requires one command. Change the Target-DC to the name Domain Controller you’re moving the FSMO role(s) to.

There are 5 FSMO roles include the ones you want to move.

Move-ADDirectoryServerOperationMasterRole -Identity "Target-DC" -OperationMasterRole SchemaMaster,RIDMaster,InfrastructureMaster,DomainNamingMaster,PDCEmulator


Recover Deleted Items from Exchange Dumpster

I was having a conversation at lunch with a friend who needed to recover some items for a user from the Exchange Dumpster. So I came up with a one-liner to help you do just that but BEFORE i can really give you the one-liner I need to give you some background.

First there is a Deleted Items folder in your Exchange Mailbox. When you delete an email it goes here first (for many people that is as far as it goes but that’s another blog post…).
A user can simply look here in the their deleted Items folder and find something they have deleted if that folder has not yet been emptied.

If the Deleted Items folder has been emptied it will remain in the Deletions folder (Dumpster) for the next 14 days by default. During this time the user can use Outlook and or OWA to Recover Items that are now in the Dumpster.  I love this feature but it’s not very much fun for the user if they have deleted a lot of items lately. 

So to make things a little easier on the user you can recover all the items in PowerShell and then export them so the user can sort them to their hearts content.  The tricky part here is that you can’t drop them directly back into the mailbox you’re searching.  You can use the DiscoverySearchMailbox but I keep an admin mailbox around that I use for just such occasions. I call this mailbox SearchAdmin and it will become the Target mailbox.

The PowerShell command looks like this:

Search-Mailbox -identity ebuford -SearchDumpsterOnly -TargetMailbox SearchAdmin –TargetFolder ebufordDumpster

Search-Mailbox -identity ebuford -SearchDumpsterOnly -TargetMailbox SearchAdmin –TargetFolder ebufordDumpster

The three items in red are user mailbox you’re searching (ebuford) the target mailbox your dropping the files in (SearchAdmin) and the name of the Folder you want to dump them in (ebufordDumpster).

Once you’ve got them in the new folder you can export to a PST and then Import them back into the users mailbox. Now this isn’t the most straightforward admin task you’re going to do, but if you really want to please a user (or maybe your boss) this will make you some brownie points!


Get your top 5 Errors out of your Event logs

So many times I want to know what Errors are filling up the logs on a server.
Here’s a quick PowerShell to show you the top 5 Errors in your Application or System Logs.
Use this one for Application Log Errors:

Get-EventLog -LogName Applicaiton -EntryType error | Group source,eventid | Sort count -Descending | select -First 5 | FT count,Name

And this one for System Log Errors:

Get-EventLog -LogName system -EntryType error | Group source,eventid | Sort count -Descending | select -First 5 | FT count,Name


Office 365 PowerShell

So you’ve setup Office 365 in the Cloud – Let me be the first to congratulate you!
Kudos!   Now that that’s over you need to connect your workstation to it using PowerShell, so let’s get started.
You need to download and install a couple of files the first one is the Microsoft Online Services Sign-In Assistant for IT Professionals, download it here.

Once that is installed you need to install the Windows Azure AD Module. Go here and choose your OS (32 or 64 bit). Download and install the Module and you’re ready to get started.

Now all you have to do is connect your workstation to Office 365. Open PowerShell and type these commands:

Import-module MSOnline

This will import the Module you’ll need to make a connection to the Office 365

$Cred = Get-Credential

This will pop up a credentials box for you to put your Administrator UPN for Office 365 in.


Next create a new session based on your credentials:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Cred -Authentication Basic –AllowRedirection

You’ll get a warning to tell you that you’re being redirected


Now Import a new PSSession based on the session you just created in the cloud.

connect-msolservice -credential $cred

And finally connect to the MS Online Service:

connect-msolservice -credential $cred

So to see the commands available now type:

Get-Command -Module msonline


Import / Export PST files with Exchange 2010 and 2013

For many reasons we sometimes need to Import or Export all or part of a mailbox to or from a PST.  Before you can Import or Export you’ll need to have permission to actually SEE the commandlets in Exchange.

So start by getting the proper permissions you can give them to a user, in this case to ebuford:

New-ManagementRoleAssignment –Role "Mailbox Import Export" –User ebuford

If you’d rather give permissions to a security group like Administrators you can do that too:

New-ManagementRoleAssignment -Role "Mailbox Import Export" -SecurityGroup Administrators

Once you’ve given rights you’re going to need to log out and then log back in to see the commandlets.

If you’d like to know who has the role assigned to them:

Get-ManagementRoleAssignment -Role "mailbox import export"

Ok so now we have the role let’s get busy!

Let’s say you need to export a full mailbox to a PST here’s how we’ll tackle that. We will need to create an new export request using the New-MailboxExportRequest  commandlet.
Specify the username for the mailbox and then give a full UNC path for the PST file you’re exporting. You can’t use C:\PSTs\ebuford.pst it must be a full UNC path.  So if you’re trying to get to the PSTs folder on the C:\ drive of your exchange server named Exchange2013 then try this:  \\Exchange2013\C$\PSTs\ebuford.pst 

-Mailbox username -FilePath \\files\pstarchive

New-MailboxExportRequest -Mailbox ebuford –FilePath “\\FileServer\PSTs\ebuford.pst”

ok so you started the mailbox export and you want to see how it’s doing. You can get the stats for a single mailbox export like this:

Get-MailboxExportRequestStatistics ebuford\mailboxexport

But what if you have a few exports running at the same time?  Try this:

Get-MailboxExportRequest | Get-MailboxExportRequestStatistics

Ok what about Importing a PST?
Well it’s basically everything we just learned but we’re going to use the NewImportRequest Commandlet.

You can also use the Get-MailboxImportRequest and Get-MailboxImportRequestStatistics.

So far so good – now let’s talk a bit about some of the options for these commands.

Let’s say I’m exporting a pst but I don’t want objects from the deleted items folder. I can use the –ExcludeFolders parameter like this:
New-MailboxExportRequest -Mailbox ebuford  – ExcludeFolders #DeletedItems# –FilePath “\\FileServer\PSTs\ebuford.pst 

Make sure you place ## around the folder

Another option might be to only get the Inbox from a mailbox you can do this just as easily using the –IncludeFolders parameter like this:

New-MailboxExportRequest -Mailbox ebuford  – IncludeFolders #Inbox# –FilePath “\\FileServer\PSTs\ebuford.pst 

Here is a list of well- know folders:

  • Inbox
  • SentItems
  • DeletedItems
  • Calendar
  • Contacts
  • Drafts
  • Journal
  • Tasks
  • Notes
  • JunkEmail
  • CommunicationHistory
  • Voicemail
  • Fax
  • Conflicts
  • SyncIssues
  • LocalFailures
  • ServerFailures

Exchange Server Message Tracking

This morning an engineer came to me with some questions about message tracking in Exchange. I did 2 things for him I logged onto a server showed him how to get started with message tracking in PowerShell.  Then I sent him over to Paul Cunningham’s blog ExchangeServerPro to download the “Become a Message Tracking Master book”. Yes you have to register to become a member, but it’s free and if you’re reading this you will want to that anyway.

The free book is invaluable, Go get it!


Setting Mailbox Database Limits with PowerShell

While doing an install or migration of Exchange to a new server one of the things I often need to do is configure all the mailbox databases at once to unlimited.
Now this is something you can certainly in the GUI but if you have more than 1 database to change then the GUI gets old fast.
You can do this easily in the Exchange Management Shell.

First you might want to record the current settings. I would run this Command to view the current settings:
*in each of these examples change SERVERNAME to the name of your server.

Get-MailboxDatabase -Server SERVERNAME | FL name,issuewarningquota,prohibitsendquota,prohibitsendrecievequota

To set the Issue Warning Quota:

Get-MailboxDatabase -Server SERVERNAME | Set-MailboxDatabase -IssueWarningQuota unlimited


To Set the Prohibit Send Quota:

Get-MailboxDatabase -Server SERVERNAME | Set-MailboxDatabase -ProhibitSendQuota unlimited


To Set the Prohibit Send / Receive Quota:

Get-MailboxDatabase -Server SERVERNAME | Set-MailboxDatabase -ProhibitSendReceiveQuota unlimited


IF you want to set all at the same time:

Get-MailboxDatabase -Server SERVERNAME | Set-MailboxDatabase -IssueWarningQuota unlimited  -ProhibitSendReceiveQuota unlimited -ProhibitSendQuota unlimited