Recreating local folder structure with PowerShell

  • Sandro Pereira
  • Dec 20, 2020
  • 3 min read

When migrating your BizTalk Server environment or deploying to a new or different environment, there are many different resources or configurations that you need to take into considerations like:

  • Local queue creations.
  • Cloud queue creations.
  • Folder creations.
  • And so on.

One of the common resources that we use on our integration solutions is local folders, whether for archiving or routing messages. I do not mean that it is a good practice or not. Whether you should do it or not, this is not the goal, only that it is common to happen.

In a first analysis, we could think that the quickest and most effective solution would be to copy and paste the folder structure from one environment to another. Still, it may not be the best solution in many cases since it may contain thousands of documents that are unnecessary to copy/migrate.

This blog post will address how we can easily recreate a folder structure in a different environment/server using PowerShell.

📝 One-Minute Brief

Shows how to recreate an existing local folder structure using PowerShell by generating a script that rebuilds directories on a different server or environment without copying unnecessary files.

PowerShell script to recreate a local folder structure

With this PowerShell sample, we will be able to recreate an existing local folder structure on a different BizTalk Server environment.

$folderList = Get-ChildItem -Path $path -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName
 
foreach ($folder in $folderList)
{
    $powerShellCommand = 'New-Item -ItemType Directory -Path "'+$folder.FullName+'"'
    Add-Content -Path $scriptPath -Value $powerShellCommand
}

The output of this PowerShell script will be a creation of a different PS script containing the instructions that you can use to recreate all the folder structures in different environments.

New-Item -ItemType Directory -Path "D:\BiztalkFilePorts\APP1"
New-Item -ItemType Directory -Path "D:\BiztalkFilePorts\APP2"
New-Item -ItemType Directory -Path "D:\BiztalkFilePorts\APP3\Inbox"
New-Item -ItemType Directory -Path "D:\BiztalkFilePorts\APP3\Outbox"
New-Item -ItemType Directory -Path "D:\BiztalkFilePorts\APP3\Outbox\FLD1"
New-Item -ItemType Directory -Path "D:\BiztalkFilePorts\APP3\Outbox\FLD2"
New-Item -ItemType Directory -Path "D:\BiztalkFilePorts\APP4\Inbox"
New-Item -ItemType Directory -Path "D:\BiztalkFilePorts\APP4\Outbox"

Download

THIS POWERSHELL IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND.

You can find this PowerShell script on GitHub:

Hope you find this helpful! If you liked the content or found it useful and would like to support me in writing more, consider buying (or helping to buy) a Star Wars Lego set for my son. 

Thanks for Buying me a coffe
Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

Your email address will not be published. Required fields are marked *

The Ultimate Cloud
Management Platform for Azure

Supercharge your Azure Cost Saving

Learn More
Turbo360 Widget

Back to Top