How to Use PowerShell to backup and delete AX application and database

There are many VPCs running in our Virtual Server for the Client Development purpose. As we develop a lot on the AX applicatoins, we need to backup the application and database everyday and also before the main changes in AX.

Here is the powershell scripts we are using to backup AX Database and application backup files.

1. Run the SQL Server maintenance Plan to set up the automatic SQL Server backup.

2. create the Powershell Scripts in the beneath: 

// You can also copy the files to some folders if you don’t like to map network driver

net use Z: \\\databasebackup pass@word1 /user:Contoso\administrator

move-item -path Z:\* -Destination C:\DatabaseBackUp

net use Y: \\\AX593 pass@word1 /user:Contoso\administrator

$a = (get-date).month

$b= (get-date).day

new-item C:\Test$a$b -type directory

copy-item -Recurse Y:\* C:\Test$a$b

$Now = Get-Date

//only keep the last three days backup.


$TargetDatabaseFolder = C:\DatabaseBackUp


$LastWrite = $Now.AddDays(-$days)

Get-ChildItem $TargetDatabaseFolder | where {$_.LastWriteTime -le "$LastWrite"} | remove-item

Get-ChildItem $TargetApplicationFolder | where {$_.LastWriteTime -le "$LastWrite"} | remove-item -recurse –force

3. Add the Powershell scripts to the Task Manager to run everyday or weeekdays.

This entry was posted in Dynamics AX and SQL Server. 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s