Skip to main content

Introduction to Scripting: Build Confidence with Command-Line Tools in macOS - Part 1

By: Kyle Bareis, Mac Solutions Architect

Summary

Welcome to our new Introduction to Scripting series! This series of articles will empower IT administrators to build confidence with scripting and programming logic. It highlights tools built into macOS and introduces new functionality for experienced script writers.

Many IT administrators begin managing devices with off-the-shelf tools. These include Apple Configurator or a Device Management Service. As deployments mature, requirements grow. Scripts are a fantastic force multiplier for meeting specific organizational needs. This series bridges the gap between getting started and advancing your deployment.

Our Challenge

Throughout this series, we explore a practical example and solve it using command-line tools built into macOS. We start with the basics and focus on the ideal path. Once the fundamentals work, we add features, verification, and documentation as we encounter unexpected issues.

Here is our challenge: During a computer refresh across campus, we want to verify that displays meet a minimum screen resolution threshold. Our school uses many Mac mini computers in classrooms, labs, and shared spaces. These devices connect to various screens and projectors purchased from different vendors over many years.

We could walk to each room and check manually. Instead, we use automation. This approach saves time now and remains useful for future refreshes.

Requirements for the Series

To follow along, gather the following hardware and software:

  • Obtain a test Mac or virtual machine running macOS 15 or later. This serves as your primary test environment
  • Prepare a secondary test Mac or virtual machine running macOS 14. Later parts of the series require this
  • Connect a primary monitor if using a Mac without a built-in display
  • Connect a secondary monitor for use in later parts of the series
  • Create a test user with administrator permissions
  • Terminal application
  • Use TextEdit or another text editor capable of writing or editing scripts

What You Will Learn

In Part 1, you learn key concepts for running commands, modifying data, and extending these skills to solve your own challenges. Key topics include:

  • Locate Terminal and System Information in macOS
  • Understand system_profiler: Learn what it does, how to run it, and why it matters
  • Output data to a text file
  • Find documentation for command-line tools
  • Modify commands using options or flags
  • Parse output data to meet specific requirements

Open Terminal

To find display information across campus, start by investigating a single device. Once you confirm the data is accessible, you can build your script. Use command-line tools to gather data programmatically.

Tip: Verify your work with other tools to ensure accuracy. System Settings provides a useful reference for confirming command-line output.

On your test device, follow these steps:

  • Open Finder
  • Navigate to the Applications folder
  • Open the Utilities folder
  • Double-click Terminal 

Tip: Customize text and background colors in Terminal. Select Terminal in the menu bar, then choose Settings. Open the Profiles tab to modify the default profile or select a different one.

Run System Profiler

With Terminal open, explore a powerful tool called system_profiler. This tool displays detailed information about hardware, software, and network configuration.

You may recognize this data from System Information, a graphical app in the same Utilities folder. System Information displays much of the same content.

Type the following command in Terminal and press Return:

system_profiler

The output contains extensive information. On most devices, the command takes one to two minutes and generates hundreds of thousands of lines. Scroll through the output to explore. Open System Information alongside Terminal and compare both views.

Save Output from System Profiler

Scrolling through this output to find display resolution takes time. Save the information to a file and search it with a text editor like TextEdit.

Add a greater-than symbol (>) after the command, followed by a file path:

system_profiler > ~/Desktop/ system_profiler_output.txt

Or specify the full path:

system_profiler > /Users/username/Desktop/ system_profiler_output.txt

The tilde (~) represents the current user's home folder. It works well for manual testing. However, avoid using it in scripts. When a script runs remotely through a device management service, the executing user determines the path. This may not match your intended destination. We cover best practices in detail later in the series.

Find Display Information

Open the text file in TextEdit or a similar app. Search for "Graphics/Displays" to find a section similar to this:

Graphics/Displays:

    Apple M1:

      Chipset Model: Apple M1
      Type: GPU
      Bus: Built-In
      Total Number of Cores: 8
      Vendor: Apple (0x106b)
      Metal Support: Metal 3
      Displays:
        Color LCD:
          Display Type: Built-in Retina LCD Display
          Resolution: 2560 x 1600 Retina
          Main Display: Yes
          Mirror: Off
          Online: Yes
          Automatically Adjust Brightness: Yes
          Connection Type: Internal

Your output may differ based on your configuration. You may see multiple displays, different resolutions, or various connection types.

Access Command-Line Tool Documentation

You now know that system_profiler provides display information. Next, learn how to retrieve only the display resolution as efficiently as possible.

Most command-line tools include built-in documentation. Access it using one of these commands:

system_profiler -help
system_profiler -h
man system_profiler

Tip: When using man system_profiler, scroll with your mouse, trackpad, or arrow keys. Press q to exit. Some tools support -help, -h, or manual pages. Built-in tools typically include manual pages. Third-party tools more commonly use -help or -h.

The output resembles the following:

Usage: system_profiler [-listDataTypes]
       system_profiler [-xml] [-timeout n] [-detailLevel n]
       system_profiler [-xml] [-timeout n] [dataType1 ... dataTypeN]

  -detailLevel n    specifies the level of detail for the report
                      mini = short report (contains no identifying or personal information)
                      basic = basic hardware and network information
                      full = all available information

  -listDataTypes    lists all the available datatypes

  -xml              generates xml output instead of plain text

  -json             generates json output instead of plain text

  -timeout          specifies the maximum time to spend gathering information
                    the default is 180 seconds, 0 means no timeout

Examples:

  system_profiler
     Generates a text report with the standard detail level.

  system_profiler -detailLevel mini
     Generates a short report without identifying/personal information.

  system_profiler -listDataTypes
     Shows a list of the available data types.

  system_profiler SPSoftwareDataType SPNetworkDataType
     Generates a text report containing only software and network data.

Review the options and flags. Identify which ones might reduce the output. Explore the examples before continuing.

Reduce Command Output with Flags

The -listDataTypes option shows available data types. Run this command:

system_profiler -listDataTypes

Review the output and find a data type relevant to display information.

Learning Challenge!

Use the help documentation to craft a command that retrieves only display information. Consider these questions:

  • Did one approach run faster than another? By how much?
  • Would command performance affect someone working on the device?
  • Is the data easier to find now?

If you did not get the expected output, that is fine. Working with command-line tools involves trial and error. Always test commands on a nonproduction device first.

The following shows a trimmed response from -listDataTypes and the focused display output:

localadm@MacBook-Air ~ % system_profiler -listDatatypes
...
SPFontsDataType
SPFrameworksDataType
SPDisplaysDataType
SPHardwareDataType
SPInstallHistoryDataType
...

localadm@MacBook-Air ~ % system_profiler SPDisplaysDataType
Graphics/Displays:

    Apple M1:

      Chipset Model: Apple M1
      Type: GPU
      Bus: Built-In
      Total Number of Cores: 8
      Vendor: Apple (0x106b)
      Metal Support: Metal 3
      Displays:
        Color LCD:
          Display Type: Built-in Retina LCD Display
          Resolution: 2560 x 1600 Retina
          Main Display: Yes
          Mirror: Off
          Online: Yes
          Automatically Adjust Brightness: Yes
          Connection Type: Internal

Extract Only the Display Resolution

Now process the focused output to retrieve only the display resolution. Use a command-line tool called grep.

Many text-processing tools exist in Terminal. Here are the most common:

  • awk
  • grep
  • sed
  • cut
  • tr

The grep command works like Find (Command + F) in an app. Provide a string to search for, and grep returns matching lines. Note that grep is case-sensitive. Multiple matches return multiple lines. For now, we focus on a single display.

To use grep:

  • Start with the command that generates your data
  • Add a space, a vertical bar (|), and another space. The pipe takes output from the left command and sends it as input to the right command
  • Add grep followed by the search term

Run this command:

system_profiler SPDisplaysDataType | grep "Resolution"

Tip: When searching text, enclose your search term in quotes. This preserves spaces and prevents misinterpretation.

The output resembles:

localadm@MacBook-Air ~ % system_profiler SPDisplaysDataType | grep "Resolution"
          Resolution: 2560 x 1600 Retina

Wrap Up and Next Steps

Even though this activity may seem small, you have made significant progress! From opening Terminal to using command-line tools and parsing data, you now have foundational skills that will be used throughout this series.

To recap, you learned how to:

  • Locate Terminal and System Information in macOS
  • Use system_profiler to gather device data
  • Output data to a text file
  • Access documentation for command-line tools
  • Modify commands with options and flags
  • Parse output to extract specific information

In the next post, we explore additional flags for system_profiler, use advanced search methods, and create a small script.

Until then, practice these concepts:

  • Explore other data types. Use --listDataTypes to find information relevant to your environment
  • Read documentation. Use -help, -h, or man to learn about grep and other commands
  • Consider resolution versus pixels. Does Retina display output differ from standard display output?
  • Experiment. Find the serial number, current wireless network, or current macOS language using your new knowledge

Thanks for reading! We look forward to helping you build a stronger Mac deployment through the power of scripting.

0 replies

This post contains content from YouTube.

If you choose to view this content, YouTube may collect and process certain personal data. You can view YouTube’s <a href="https://www.youtube.com/t/privacy" target="_blank">privacy policy here<span class="a11y">(opens in new window)</span>.</a>

This post contains content from YouTube.

You have rejected content from YouTube. If you want to change your consent, press the button below.