Category filter

PowerShell scripts to set or get event log properties

The document helps you with a PowerShell script to set or get event log properties on Windows devices.

Windows logs record detailed information about system events, errors, and application activities. They are categorized into three primary types: Application, Security, and System. The Application log records events related to applications, including errors, warnings, and informational messages. In contrast, the Security log captures security-related events such as login attempts and user account modifications. Finally, the System log encompasses events logged by system components and services, such as driver failures and system startup/shutdown events.

IT administrators often need to manage and monitor logs to track user activity, such as login/logout times, application errors, or system warnings. It is also necessary to manage log properties that define various attributes associated with log entries generated on the devices assigned to the users. The scripts described below let you set/get log properties like maximum log size and retention period for Windows. By setting maximum log size, IT administrators can avoid unnecessary resource consumption. When logs reach the maximum limit, older logs will be rewritten by newer ones. Likewise, setting a retention period helps determine how long the log data should be stored before being automatically deleted or archived. You can use the ‘Execute Custom Script’ action. action to execute such customized scripts on the devices.

Disclaimer:

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

Script to set log properties

The PowerShell script to set event log properties facilitates the customization of maximum log size and retention period for Windows event logs. The retention period is the duration (in days) the log data is stored before deletion or archiving, while the maximum size (in megabytes) is the allocated disk space for log storage.

IT administrators can specify (as $logName) the type of log they want to configure these settings for Application, Security, or System. The Set-LogProperties function takes parameters such as log name ($logName), maximum log size ($maxSize), and retention period ($retainDays) to configure log properties within the Windows Registry.

After the execution of the script, the new values for the maximum size and retention period will be updated in the registry, and all options in the Log Properties of the specified Windows log will be greyed out.

When the script to set event log property is executed, all options within the log properties will be greyed out

Script to get log properties

The PowerShell script to get event log properties retrieves and displays properties, namely the maximum size (in megabytes) and retention period (in days), for the specified Windows event log. The Get-LogProperties function takes the log name as a parameter, retrieves the corresponding registry path, and utilizes Get-ItemProperty to fetch the maximum log size and retention period.

Following the execution of the script, the log properties will be displayed in the Action History of Hexode UEM.

Fetching the event log properties and displaying their values

Notes:

  • 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