Tip of the Week #87 – How to Remove Unused Companies from a Database

Tip of the Week #87 – Easily Remove Unused Companies from a Database

Developer Peter Tijsma offers up the first helpful tip of 2017: “YAPS (Yet Another PowerShell Script)” — perfectly timed for the new year, when most of us resolve to rid some unwanted clutter from our lives.

How to easily remove a bunch of companies from a database.

We often need to remove a lot of companies from a database because we create many of them for testing. Since removing a company through the Windows Client is a tedious task, it makes much more sense to have the system do it for you in an easy way. 🙂

This is again where PowerShell comes in handy.

Simply use the script below, specify 2 variables and run it. This will remove all companies from your database that you don’t want anymore.

In this example script we always leave the Cronus companies in, but of course you’re free to alter it to your own needs.

The values for 2 variables that needs to be modified are:

  1. $InstanceName
    This is where you specify the name of the Dynamics NAV Service Instance (NST)
  2. $KeepCompanyName
    In this one you specify the name of the company in the database which you want to keep. All the others will be removed.

Be careful with the script, since it WILL remove companies without warning.

The script itself:

Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\100\Service\Microsoft.Dynamics.Nav.Management.dll’

$InstanceName = ‘DynamicsNAV100’

$KeepCompanyName = ‘My Preferred Company Name’

$Companies = Get-NAVCompany -ServerInstance $InstanceName | Where { $_.CompanyName -notlike ‘CRONUS*’ -and $_.CompanyName -ne $KeepCompanyName }

foreach ($company in $Companies) {

$companyName = $company.CompanyName

Write-Host “Removing Company: $companyName …”

Remove-NAVCompany -ServerInstance $InstanceName -CompanyName $companyName -Force



Thank you Peter!

Call Now Button