Category filter

Script to convert text between uppercase and lowercase on Windows

While creating, formatting, and editing documents, you often need to change the text case of any given text from uppercase to lowercase or vice-versa. Though there are several case conversion tools that will help you achieve it, you are required to copy-paste the text to convert it. But, if you would want to fetch the original text from a file present on a device, transform it to the required case and replace the file with the modified text, this script can help you. It works across the files stored on Windows and converts the text into the desired case – uppercase or lowercase. The Execute Custom Script action lets you execute these customized scripts on different endpoints remotely.

Disclaimer:


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

PowerShell script

Change text to uppercase

  • param() – The param() function defines $path_to_file as an argument.
  • $path_to_file – Specifies the path to the file where the text is stored.
  • Get-ChildItem – Retrieves a list of child objects (folders, files, etc.) within the specified directory.
  • Get-Content – Retrieves the content of a specified file.
  • ToUpper() – Returns a copy of the string converted to uppercase.
  • Out-File – Writes the output to the file whose path is provided.

For example, to change the text case of the contents stored in a file named ‘Hexnode.txt‘ to uppercase, provide the path to the file, ‘C:\Users\Admin.HPNoteBook\Desktop\Hexnode.txt‘ as an argument while deploying the script:

Specify the path of the text file as an argument to convert it to uppercase

The script will fetch the contents from the file, change them to uppercase, thus replacing the file contents with it.

Text file after execution of the script that converts the text in a file to uppercase

Change text to lowercase

  • param() – The param() function defines $path_to_file as an argument.
  • $path_to_file – Specifies the path to the file where the text is stored.
  • Get-ChildItem – Retrieves a list of child objects (folders, files, etc.) within the specified directory.
  • Get-Content – Retrieves the content of a specified file.
  • ToLower() – Returns a copy of the string converted to lowercase.
  • Out-File – Writes the output to the file whose path is provided.

Consider an example. To convert the text case of the contents located in a file named ‘Hexnode.txt‘ to lowercase, specify the file path ‘C:\Users\Admin.HPNoteBook\Desktop\Hexnode.txt‘ as an argument while executing the script:

 Specify the path of the text file as an argument to convert it to lowercase

The script will fetch file contents, convert them to lowercase, and replace the file contents with it.

Text file after execution of the script that converts the text in a file to lowercase

Note:

  • The “Get-Content” cmdlet supports file that is in plain text format, such as .txt, .csv, .log, .xml, .html, .ps1, .psm1, .json, etc. But, it is not recommended to use these scripts on files that have binary values present in them.
  • 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.

  • Sample Script Repository