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.
PowerShell script
Change text to uppercase
1 2 3 4 5 6 7 |
param( [Parameter()] [String]$path_to_file ) Get-ChildItem $path_to_file -Recurse | foreach{ (Get-Content $path_to_file).ToUpper() | Out-File $path_to_file } |
- 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:
The script will fetch the contents from the file, change them to uppercase, thus replacing the file contents with it.
Change text to lowercase
1 2 3 4 5 6 7 |
param( [Parameter()] [String]$path_to_file ) Get-ChildItem $path_to_file -Recurse | foreach{ (Get-Content $path_to_file).ToLower() | Out-File $path_to_file } |
- 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:
The script will fetch file contents, convert them to lowercase, and replace the file contents with it.