Otto  background

Automox Script: Remove Devices That Have Been Disconnected Longer Than X Days

Connect With Us

Start now, and patch, configure, and control all your endpoints in just 15 minutes.

Automox Script Overview: Remove Devices That Have Been Disconnected Longer Than X Days

A common request we get is to remove old devices from the console, especially ones that might have been re-imaged. Currently you can do this on the Devices page by sorting by Last Disconnected Date and selecting devices to remove. However, this can become unwieldy if you have pages and pages of devices to remove.

This script automates the removal using an API call to check the last disconnected date and remove any devices older than the number of days you specify in the code.

There are four areas in the code you’ll need to update to get the script to function:

  • $orgID = 'YOUR_ORG_ID' - put your Org ID, which can be found by looking at the URL of your console and selecting the value after the “?o=”: https://console.automox.com/dashboard?o=999999. In this example URL, the Org ID is the 999999 portion.
  • $apiKey = 'YOUR_API_KEY' - in your console, go to Settings->API and select the API key. Note that the API key is per admin user, so you and another admin in your console will have different API keys.
  • $maxDays = 120 - any device that has been disconnected for more than 120 days will be deleted from the console. You can adjust this to the number of days you prefer.
  • $logPath = 'C:\temp\' - set this to the folder of your choosing

Once you’ve made those four changes, you can run the script below on any Windows device using Powershell.

Note: This script will automatically remove devices from your console and this is not a reversible operation. Any device that you remove unintentionally using this script will have to have the agent reinstalled to return it to your console.

If you wish to run the script in a test mode to see what will be deleted, you can uncomment this line:

#echo "device: $serverName `t last disconnected date: $lastCheckin `t days disconnected: $span.Days"

and comment out the line that does the deletion:

$delResponse = Invoke-WebRequest -UseBasicParsing -Method Delete -Uri $delURI

That will show you a list of all your devices with the date of last disconnect and number of days since that disconnect, and which devices would have been deleted by the script.

To run this script, use the remediation code script located here in the original posting on the Automox community.

Tips for Creating an Automox Worklet

Before deploying an Automox Worklet to the production environment, we suggest testing this on a few devices to confirm its accuracy. If you have any questions, please contact our support team for technical assistance at support@automox.com.

For step-by-step instructions on creating the Worklet, see our user documentation: Create a Worklet.


Automox for Easy IT Operations

Automox is the cloud-native IT operations platform for modern organizations. It makes it easy to keep every endpoint automatically configured, patched, and secured – anywhere in the world. With the push of a button, IT admins can fix critical vulnerabilities faster, slash cost and complexity, and win back hours in their day. 

Grab your free trial of Automox and join thousands of companies transforming IT operations into a strategic business driver.