AX management shell – multiple models

Even if you’re not familiar with PowerShell, you probably don’t have much trouble with using AX management cmdlets. You just call the right command, set some parameters and it’s done.

# Just for example
Export-AXModel -Model "MyModel" -File "c:\my.axmodel"

If you don’t know the parameters, you’ll simply use Get-Help or some of its aliases, such as man:

man Export-AXModel -Detailed

But if you don’t know PowerShell well, you may not know how, say, to work with multiple models at once. This is often useful, therefore it’s worth showing how to do it.

Before we look at any code, ensure yourself that you have a development environment such as PowerShell ISE or PowerGUI. These applications will help you saving your scripts, discovering commands and parameters, setting breakpoints, reviewing variables and many other things. No need to do everything directly in console!

But there is a problem – if you try to use AX cmdlets outside AX management shell, they won’t be recognized. Fortunately the fix is easy – add the following line to your script (including the dot at the beginning):

. 'C:\Program Files\Microsoft Dynamics AX\60\ManagementUtilities\Microsoft.Dynamics.ManagementUtilities.ps1'

You can also run it directly in console or even to add it to your profile, so it’s loaded automatically every time you run PowerShell.

Let’s warm up with a few simple commands (using PowerShell 3.0 syntax):

# Lists all AX modules - nothing new here
# Lists just model names and layers, to make things more readable
Get-AXModel | select Name, Layer
# Lists models in a specific layer
Get-AXModel | ? Layer -eq USR

The “| ” symbol is a pipe and it passes the output of one command to the input of another command. It would be great if could use it with AX cmdlets. e.g. for passing a list of models to Export-AXModel. Unfortunately it’s not supported and AX cmdlets also don’t accept arrays as arguments, therefore we have to call each command for a single model at a time. It’s not that bad as it might sound, because we can use a loop to sequentially execute a command for every element in an array.

# Let's set some parameters first 
$toDir = 'C:\Models'
$layer = 'USR'
# Export all models in a given layer to file
Get-AXModel | ? Layer -eq $layer | % { Export-AXModel -Model $_.Name -File "$toDir\$($_.Name).axmodel" }
# Uninstall all modules from a given layer
Get-AXModel | ? Layer -eq $layer | % { Uninstall-AXModel -Model $_.Name -NoPrompt }

It might look a little bit cryptic at first, but you see it’s rather simple and you shouldn’t have problems to apply the same approach in your own scripts. It’s much faster to write (or copy) something like this than typing, running and waiting for several individual commands.


  1. I know this is an old post, but using the Get-AXModel would it be possible to export only models that are not empty and remove the once that are empty? Hotfix models might become empty overtime.

    • I seems to have found something useful:
      $toDir = ‘C:\temp’
      $layer = ‘SYP’
      $buildno = ‘6.3.6000.151’

      Get-AXModel | ? version -GT $buildno | ? Layer -eq $layer | ? elementcount -gt 0 | % { Export-AXModel -Model $_.Name -File “$toDir\$($_.Name).axmodel” }

      This exports all models with build number later than CU13 in the SYP layer that are not empty.

Comments are closed.