Move Hallengren Backups to instance folder

The Ola Hallengren backup solution creates folder names of the concatenation of SERVER + $ + INSTANCE.

I wanted to move these folders into a parent directory of INSTANCE. That is because I have several servers with the same instance name, and this is how I want it organized.

The dollar sign has to be handled in Powershell. This code uses the REPLACE function to replace the $ with `$. The backtick is the escape character for dollar sign.

The script finds the substring after the dollar sign, and creates a folder of that name. Then it moves the original folder into the new folder.

$dir = "\\FileServer\SQLBackups\Product";

$NameGroup = Get-ChildItem $dir | select name;

foreach($Name in $NameGroup) {

$NewDir = $Name.Name;
$SubDir = $NewDir.Substring( $NewDir.IndexOf("$") + 1, $NewDir.Length - $NewDir.IndexOf("$") - 1);

$OldFullFolder = $dir + "\" + $NewDir;
$OldFullFolder.Replace("$", "`$");
$NewFullFolder = $dir + "\" + $SubDir;

New-Item $NewFullFolder -type directory;
Move-Item $OldFullFolder $NewFullFolder;

This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s