Laeeq Qazi's Exchange Server Blog

Exchange Server blog

How to recreate Primary Hierarchy Public Folder Mailbox in Exchange 2013


Sometimes, in our lab, developers delete Primary Public Folder Hierarchy Mailbox by mistake, and after it, we cannot perform any public folder operation via PF cmdlets.

E.g. a get-publicFolder cmd thows this error:

[PS] C:\Windows\system32>Get-PublicFolder \ -GetChildren
Couldn't find the public folder mailbox.
+ CategoryInfo : NotSpecified: (:) [Get-PublicFolder], ObjectNotFoundException 


This is how I first confirm that ‘Primary Hierarchy Public Folder Mailbox’ has been deleted:

First I get the Primary Hierarchy PF mailbox using this command

Get-OrganizationConfig | FL RootPublicFolderMailbox

RootPublicFolderMailbox represents GUID of PF mailbox.

When I retrieved all PF Mailboxes, Guid of RootPublicFolderMailbox was not there:

Get-Mailbox –PublicFolder | FL Name,ExchangeGuid

which confirms that ‘Primary Hierarchy Public Folder Mailbox’ is no more in Exchange Server 2013, and someone has deleted it.

Recreation of Public Folder Hierarchy Mailbox:

The only way to recreate ‘Primary Hierarchy PF Mailbox’ I discovered via this helpful blog
which is to remove all Public folders, all Secondary Public Folder Mailboxes, but I was not able to remove Public Folders and getting this error.

[PS] C:\Windows\system32> Get-PublicFolder -Recurse | Remove-PublicFolder
Couldn't find the public folder mailbox.
+ CategoryInfo : NotSpecified: (:) [Get-PublicFolder], ObjectNotFoundException

So in the end, what I did was ‘Remove AD users associated with all Secondary PF Mailboxes using ‘Active Directory Users and Computers’ console.

For this, first I got the location of all secondary PF Mailboxes like this:

Get-Mailbox –PublicFolder | FL DistinguishedName

And then Removed the associated AD user using ADUC console, which subsequently disconnected all PF mailboxes.

Then I opened ADSIEdit.msc , and went to “Cn=Configuration” container, and browsed ‘Exchange Organization’ object’s properties, whose AD Path looks like this:

CN=OrganizationName,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=AdDomain,DC=lab

From properties of this object, I just cleared the value from the attribute ‘msExchDefaultPublicFolderMailbox’, which you can see is just a garbage.

Then I just re-created the Primary Hierarchy PF mailbox using this command:

New-Mailbox -PublicFolder -Name 'Primary Hierarchy PF Mailbox'

Then I recreated my secondary PF Mailboxes, and then created Public Folders.

Note: Please test this in a lab, before applying to production scenarios. Also, This doesnt recover existing Public folders.

December 26, 2013 Posted by | Exchange 2013 | , | 12 Comments

How to log ‘Exchange Management Shell’ session in Exchange 2010\2013


Exchange admins often want to log each and every command and their result to a text file, to be used\reviewed in future.

This is very easy in Exchange 2010\2013.

Locate ‘RemoteExchange.ps1″ file in Exchange Server bin folder, which has this path with default C: drive installation:

C:\Program Files\Microsoft\Exchange Server\V15\Bin

First take a backup of this file and then Open ‘RemoteExchange.ps1″ and put this commands:

$status = start-transcript;
Write-Host -ForegroundColor Green "`n$status;"
Write-Host -ForegroundColor Green "All of your commands would be logged to above mentioned text file`n";

as the last lines in the script, after “get-tip ” command.

Save this file, and restart “Exchange Management Shell”.

Hope this helps you.

Kind Regards,

December 9, 2013 Posted by | Exchange 2010, Exchange 2013 | | Leave a comment

Cannot invoke pipeline because runspace is not in the Opened state


I developed a web app to manage exchange server 2010 objects (domains, mailboxes etc) using Remote PoewerShell API. When deployed this web app on an other fresh server with Exchange Server 2010 installed, I got this error:

Cannot invoke pipeline because runspace is not in the Opened state. Current state of runspace is ‘Broken’.

The user, which I was using for Remote PowerShell credentials, was a domain admin, with all  sorts of rights, so I looked into basic RemotePowershell configuration, and found the solution.

I followed these 2 steps to resove this error:

1. Enabled Powershell remoting on server. Open Windows PowerShell console and run


It would prompt you for input, and then just press ‘Enter’, 2 times.

2. Go to IIS manager, and under default website, locate Exchange ‘PowerShell’ virtual directory. On right side pane,  double click “Authentication” and enable “Basic” authentication.

Dont forget to reset IIS by Start->Run-> IisReset.

These 2 steps resolved my error. I would also suggest to look for 3rd step, which is to check whether the user, which you are using for Remote PowerShell credentials, has the “RemotePowerShellEnabled” flag enabled or not, which you can check via Exchange Management Shell on Exchange server by running:

get-user domain\User|fl RemotePowerShellEnabled

If it returns false, then enable it by using

set-user domain\User-RemotePowerShellEnabled:$True

Hope this help someone else too.

Kind Regards,

Laeeq Qazi

January 3, 2013 Posted by | Exchange 2010, Exchange 2013 | Leave a comment

Create Mailbox on Exchange Server 2013 using C#


Creating a mailbox on Exchange 2013 takes same source code as I used on Exchange 2010.

RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create();

PSSnapInException PSException = null;

PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException);

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf);


Pipeline pipeline = runspace.CreatePipeline();

//StringToSecureString is a function defined at the end

SecureString securePwd = StringToSecureString("Passw0rd1");
Command newMailBox = new Command("New-Mailbox");

newMailBox.Parameters.Add("Name", "TestName1");
newMailBox.Parameters.Add("Alias", "TestName1");
newMailBox.Parameters.Add("database", "Mailbox Database 1406738839");
newMailBox.Parameters.Add("Password", securePwd);
newMailBox.Parameters.Add("DisplayName", "TestName1");
newMailBox.Parameters.Add("UserPrincipalName", "TestName1@ad.lab");
newMailBox.Parameters.Add("OrganizationalUnit", "ou=myorg,dc=ad,dc=lab");
newMailBox.Parameters.Add("FirstName", "TestName1");


Collection<PSObject> result = pipeline.Invoke();

Here is the helper function

public static SecureString StringToSecureString(string str)
   SecureString ss = new SecureString();
   char[] passwordChars = str.ToCharArray();

   foreach (char c in passwordChars)
   return ss;

Note: I used VS 2010 for it. If you use any of the Exchange DLLs, like Microsoft.Exchange.Management.dll etc, then you would have to use VS 2010, as these Exchange DLLs have been developed using .Net 4.0, and you cannot add a reference of .net 4 DLL in VS 2008 or lower version of VS.

Very Important: After a healthy discussion on Exchange 2013 Forum on support of Local PowerShell sessions on Exchange 2010\2013, I don’t recommend to use above mentioned approach to create mailboxes on Exchange Server 2013, instead use Remote PowerShell sessions. I would put a post on it in coming days, where I would show how to create a mailbox on Exchange 2010\2013 using Remote PowerShell. Please visit the link mentioned in the first comment below for more details. I would also mention some problems while adopting Remote PowerShell approach like Object Deserialization issue.


Laeeq Qazi

October 2, 2012 Posted by | Exchange 2013 | , , , | 15 Comments

No snap-ins have been registered for Windows PowerShell version 3


I was developing an app to manage exchange mailboxes and having the error when running the app in VS 2010.

No snap-ins have been registered for Windows PowerShell version 3

It turned out that I was targeting x86 platform in project properties, and I think due to which app was trying to pick 32 bit powershell. I changed the target cpu to “Any CPU” and then error disappeared.

Also please look at

Hope it helps someone else too.

Note: To develop apps for Exchange 2013 preview using Exchange PowerShell sdk, you would have to use VS 2010, becuase Exchange server 2013 have been developed using .net 4.0, and you cannot add .net 4.0 DLL references in VS 2008 or lower versions.


Laeeq Qazi

October 2, 2012 Posted by | Exchange 2010, Exchange 2013 | , | 4 Comments

How to remove client permissions from a public folder and all its children?


In Exchange 2007/2010 you can remove client permissions from a public folder and all its child public folders for all users in bulk using following script.

Suppose you have a root level public folder ‘\Sales’ on public folder server “Mailbox01″, which in turn has 2 child public folders ‘\Sales\Uk’ and ”\Sales\US’, then this script will remove all client permissions from these 3 public folders for all users.

########### Script Start #####################
$AllPublicFolders = Get-publicFolder ‘\Sales’ -recurse -server Mailbox01

foreach($Pf in $AllPublicFolders ){

Get-PublicFolderClientPermission $Pf | Foreach{ Remove-PublicFolderClientPermission $_.Identity -User $_.User -AccessRights $_.AccessRights }

########### Script End #####################

Note: Put above lines of code in a text file and save it as .ps1 (powershell) file and then execute it in Exchange Management Shell (EMS) or Windows Powershell console.

If you save this file in Exchange Scripts folder
“C:\Program Files\Microsoft\Exchange Server\Scripts” [Exchange 2007]
“C:\Program Files\Microsoft\Exchange Server\V14\Scripts” [Exchange 2010]

then, in EMS, you will just need to enter the .ps1 file name and then press enter.

You might also need to set the powershell execution policy as RemoteSigned in EMS:

Set-ExecutionPolicy RemoteSigned

Also, if you decide to run this script in Windows Powershell Console then don’t forget to add powershell exchange snap-in using one of the following commands:

#For Exchange 2007:
Add-PSSnapIn “Microsoft.Exchange.Management.PowerShell.Admin”
#For Exchange 2010:
Add-PSSnapIn “Microsoft.Exchange.Management.PowerShell.E2010”

Note: For exchange 2013\Office 365, first line of script should be:

$AllPublicFolders = Get-publicFolder ‘\Sales’ -recurse


Laeeq Qazi

June 27, 2010 Posted by | Exchange 2007, Exchange 2010, Exchange 2013, Office 365 | 6 Comments