# Script to get versions of installed apps on macOS

In this document, you will learn how to create and deploy a script to get the versions of apps installed on macOS devices.

The application version helps users, developers, and support teams identify and differentiate between various releases of the same software. Identifying the currently running version of the application ensures that the applications are running according to organizational requirements. Retrieving an application version can be easy when the devices are readily available to administrators. However, in situations where the devices are remote, it can be more challenging. For macOS devices, you can retrieve the application versions of the installed apps remotely using a custom script. IT administrators can use Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) remote action to deploy the following script to get versions of installed apps on macOS devices.

Scripting language – Bash

File extension – .sh

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

Retrieving the application versions 
------------------------------------

The following script uses the **mdls** command to print the values of all the metadata attributes related to the specified applications. Then the script retrieves the **kMDItemVersion** attribute using mdls command, where the application version is stored. The script retrieves a list of specified applications with their names and versions as output. If an application version cannot be retrieved, the script returns an error message: ‘Unable to retrieve version for the specific application.’

Script to get versions of installed applications on macOS devices

\#!/bin/bash applications=("Application1" "Application2" "Application3") heading\_printed=false for app in "${applications\[@\]}"; do version=$(sudo -u Username mdls -name kMDItemVersion "/Applications/${app}.app" 2>/dev/null | grep -o '".\*"' | sed 's/"//g') if \[ -z "$version" \]; then echo "Unable to retrieve version for ${app}" else if \[ "$heading\_printed" = false \]; then echo "Application Versions:" heading\_printed=true fi echo "${app}: ${version}" fi done 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

  \#!/bin/bash 

applications=("Application1" "Application2" "Application3")

heading\_printed=false

for app in "${applications\[@\]}"; do

 version=$(sudo -u Username mdls -name kMDItemVersion "/Applications/${app}.app" 2>/dev/null | grep -o '".\*"' | sed 's/"//g')

 if \[ -z "$version" \]; then

 echo "Unable to retrieve version for ${app}"

 else

 if \[ "$heading\_printed" = false \]; then

 echo "Application Versions:"

 heading\_printed=true

 fi 

 echo "${app}: ${version}"

 fi 

done 

   

 

  

Before executing the script, make sure to replace Application1, Application2, and Application3 with the required application names. You can specify as many applications as needed. Replace *Username* with the account name of the user under which the specified applications are installed on the macOS device.

Upon successful script execution, the installed application versions will be retrieved and displayed in the Hexnode console. To view the output, navigate to the device details page by going to *Manage* > *Devices* and selecting your device. Then, click on *Show Output* for the corresponding action in the ***Action History*** tab.

[![Script to get versions of installed apps on macOS retrieves results to Hexnode console](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/07/Results-on-the-Hexnode-portal-for-the-script-to-get-versions-of-installed-apps-on-macOS-devices.png "Results on the Hexnode portal for the script to get versions of installed apps on macOS devices")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/07/Results-on-the-Hexnode-portal-for-the-script-to-get-versions-of-installed-apps-on-macOS-devices.png)

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.