Scripting best practices every IT admin must follow

The right way to get down to scripting

Selecting your scripting language

Best practices while selecting the scripting language
  • The target OS: Consider the OS platform of the device to which the script is to be pushed. There are many scripting languages, but the primary ones supported by one OS will be different from the other. For instance, in Windows, Batch and PowerShell are the major scripting languages, but for Macs, AppleScript and JavaScript are the commonly used ones. So, you should choose a scripting language that help you build better scripts on the targeted platform.
  • Ease of use: Before deciding on a scripting language, be sure to check its flexibility to take in new functionality, ease to extend and fix bugs. Choose a scripting language you’re comfortable working with and is easy to incorporate new things.
  • Community support: There are many community forums, tutorials and articles out there to support coders. Select a scripting language only if it has enough resources available and has active forums through which you can seek help if needed.
  • Processing time: The time it takes to write a script depends on the difficulty of learning and understanding the scripting language. So, consider taking a scripting language that is easy to follow and is less time consuming.

Picking up an editor

Writing down your script

Best practices while writing down the script
  • Give proper variable naming: Strictly follow the naming conventions of the selected scripting language. Keep them short and use proper capitalizations and other characters to make the names understandable. The value they store should be reflected by the variable’s name itself.
  • Show consistency in code formatting: The codes within your script should be written in a clean format with proper spacing and alignment, which is important in making the script more readable. Whatever format you use, be sure to follow the same formatting throughout your scripts so that it would be easy for you as well as others to understand and distinguish between different sections of the script easily.
  • Leave explanations of the codes via comments: Code comments are really important as they give the reader an easy understanding of what each line of code is meant for. It can even help the writer of the script at a later time to quickly recollect the logic behind the script. It is good to start the script with a comment indicating what the script actually achieves and what parameters does it use.
  • Don’t embed data directly: Generally known by the term hard coding, embedding sensitive data directly into the code can cause serious security risks. Please avoid inserting usernames, passwords, any such credential or any sort of confidential data inside scripts. Instead of directly adding the data, you can make use of parameters and share the data only when needed, which will keep the readers away from knowing or accessing your sensitive information.

Testing your script

  • Checking each line of your commands.
  • Dividing the script into small sections and test out the functionality of a small part first rather than checking the entire script at once sometimes results in spending hours only to find out that the script was wrong.
  • Using as much as possible a non-productive but similar environment for testing.
  • Testing manually before script execution in your production environment.
  • Making sure to check the expected behavior by testing on devices for which you don’t have admin rights.

Executing your script

Best practices while executing your scripts
  • Executing scripts on-demand using the terminal application
    This is the most generic method, which involves individually running the script by opening the terminal app on each of the target devices and executing the script using specific commands.
  • Scheduling script execution
    There are many options to set the script to run on a scheduled time or repeatedly run after particular intervals. There are even scheduling tools like Task scheduler for Windows and Crontab for macOS to automate script execution. However, scalability is still a matter of concern. For IT admins handling a large fleet of devices, they need to spare some time to access each device and schedule the script execution
  • Executing scripts remotely using UEM
    Running scripts is a great automation option for enterprise IT, and the ability to execute them remotely would be even greater. This is what you can achieve with a UEM like Hexnode. All your custom scripts can be uploaded to the Hexnode management portal and push remotely to your target devices individually on in bulk as and when needed. Hexnode now allows pushing custom scripts to Windows and macOS devices.
  • Scalability: The scripts can be pushed to a large number of devices at once.
  • Remote execution: This may be the most important perk of executing scripts via UEM, especially for IT admins who need to handle employee devices at remote locations.
  • Orchestration of scripts: All your scripts can be neatly arranged and kept well organized to easily identify and trigger particular scripts on demand on a user device.
  • Execution based on scope: Scripts can be executed to device groups or user groups according to their scope.
  • Conditional execution: UEM allows to create criteria-based dynamic groups and execute scripts to a specific category of devices only when they meet or violate the set conditions.
  • Ease of tracking: The status of script execution shown in the UEM portal makes it easy for the IT admin to monitor the script execution status and take necessary actions as and when needed.



Hexnode MDM is an award winning Enterprise Mobility Management vendor which helps businesses to secure and manage BYOD, COPE, apps and content.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store