Category filter

Script to add content to files in Windows devices

IT administrators can use PowerShell scripts to add content such as words to files stored on endpoint devices. You can now deploy these scripts to multiple endpoints using the Execute Custom Script action of Hexnode UEM. Adding content to these locations no longer requires user intervention or permission.


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

Add date and time to the end of a specified file

The Add-Content cmdlet adds content to an item or file whose file path is specified in the Path parameter. The Value parameter takes the output of the Get-Date cmdlet and the PassThru parameter outputs the added content to the pipeline. Finally, the Get-Content cmdlet displays the updated file with the date and time appended to the end of the file.

For example, to append the date and time of script execution to the file “Add date to file.txt” stored in the file path “C:\Users\John\Documents”:

Add-Content -Path C:\Users\John\Documents\“Add date to file.txt” -Value (Get-Date) -PassThru

Get-Content -Path C:\Users\John\Documents\“Add date to file.txt”

The script generates a new file if the specified file does not already exist on the device. The action history in the Hexnode UEM console also shows the contents of the file.

Add a string to all the text files in a directory with an exception

The Add-Content cmdlet adds the text string specified by the Value parameter at the end of all the text files in a folder. The exception is the text file that is explicitly excluded by the Exclude parameter.

E.g., Add-Content -Path C:\Users\John\Documents\*.txt -Exclude ‘Excluded text file.txt’ -Value 'End file'

This results in the “End File” string being added at the end of all the text files in the folder “Documents” except for the text file named “Excluded text file.txt”.

Copy the content of a specified file to another file

The Get-Content cmdlet gets content from an existing file specified by the Path parameter. Then, it reads the file’s contents and stores them in the variable $From. The Add-Content cmdlet copies this content to a file that already exists or is generated by the script. For this, the path of the file where the content is to be copied is specified under the Path parameter, and the $From variable is taken as the argument for the Value parameter. Using the Get-Content cmdlet, the copied content is displayed in the Hexnode UEM console.

E.g., To copy the contents of the file “Copy from File 1.txt” to the file “Copy to File 2.txt”, use the command:

$From = Get-Content -Path C:\Users\John\Documents\‘Copy from File 1.txt’

Add-Content -Path C:\Users\John\Documents\‘Copy to File 2.txt’ -Value $From

Get-Content -Path C:\Users\John\Documents\‘Copy to File 2.txt’

Add content to a read-only file

The script adds content to a read-only file that already exists or is created by the script.

Create a new file using PowerShell’s New-Item cmdlet. You may remove this cmdlet if the read-only file already exists, but the script will still work even if you don’t.

To remove the read-only attribute, use the Set-ItemProperty command with the Value parameter set to False.

Use PowerShell’s Get-ChildItem command to retrieve specific details from the file, such as Mode, LastWriteTime, Length, and Name.

The Add-Content cmdlet appends a line to the read-only file specified by the -Path parameter.

For example, the script below appends the string “Add value 1” to the read-only file “IsReadOnlyTextFile.ps1”:

New-Item -Path C:\Users\John\Documents\IsReadOnlyTextFile.ps1 -ItemType File

Set-ItemProperty -Path C:\Users\John\Documents\IsReadOnlyTextFile.ps1 -Name IsReadOnly -Value $True

Get-ChildItem -Path C:\Users\John\Documents\IsReadOnlyTextFile.ps1

Add-Content -Path C:\Users\John\Documents\IsReadOnlyTextFile.ps1 -Value 'Add value 1' -Force

Get-Content -Path C:\Users\John\Documents\IsReadOnlyTextFile.ps1

Add content to files of a specific format

Admins can use the script to add content to files of a specific format that are stored in a directory. When using filters to define the Path parameter, you must include an asterisk (*) at the end to indicate the contents of the path.

E.g., You can add a string “Text for all files in .ps1 format” to all the .ps1 files in C:\Users\John\Documents\* of the endpoint device using the command:

Add-Content -Path C:\Users\John\Documents\* -Filter *.ps1 -Value "Text for all files in .ps1 format"


  • Multiple paths can be added in the script for Get-Content and Add-Content parameters by separating the paths with a comma. Similarly, more than one string can be added to the files by using a comma.
  • 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