Category filter
Script to track app usage on Windows devices
Tracking app usage data, such as total time used and device storage occupied by each app, allows IT administrators to identify app usage patterns and make decisions to optimize the device’s performance. Removing unnecessary applications can free up storage space, improve device performance, and prevent employees from wasting work time on irrelevant apps. This document provides PowerShell scripts designed to track app usage on Windows devices. Using Hexnode’s Execute Custom Script remote action, administrators can execute these scripts on their Windows devices.
Track the usage of all Windows apps
1 2 3 4 5 6 7 |
Get-Process | Select-Object Name, MainWindowTitle, @{Name="MemoryUsage(MB)"; Expression={$_.WorkingSet / 1MB -as [int]}}, @{Name="UsageTime"; Expression={ $uptime = [DateTime]::Now - $_.StartTime "{0}d {1}hours {2}m {3}s" -f $uptime.Days, $uptime.Hours, $uptime.Minutes, $uptime.Seconds }} |
This PowerShell script retrieves a list of all currently running processes and selects specific properties to display, including the process name, memory usage in megabytes, and the total running time. The script uses the Get-Process cmdlet to get the processes and Select-Object to choose and customize the output.
Custom properties are created after executing the script: one for memory usage, converting from bytes to megabytes and casting it to an integer, and another for the usage time, which calculates the elapsed time since the process started and formats it in days, hours, minutes, and seconds.
After the execution of the above script, we will get the usage data of all apps on the device. The output of the Execute Custom Script can be viewed from the Action History tab of the device.
Track the usage of a specific Windows app
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Specify the application name $appName = "AppName" $Processlist = Get-Process # Get the process for the specified application $process = $Processlist | Where-Object { $_.ProcessName -Like $appName } # Check if the process exists if ($process) { # Get the memory usage in MB $memoryUsageMB = [math]::Round($process.WorkingSet / 1MB, 2) # Get the application usage time $usageTime = (Get-Date) - $process.StartTime # Display application name, memory usage, and usage time Write-Host "Application Name: $($process.ProcessName)" Write-Host "Memory Usage: $memoryUsageMB MB" Write-Host "Usage Time: $($usageTime.Hours) hours, $($usageTime.Minutes) minutes, $($usageTime.Seconds) seconds" } else { Write-Host "Application '$appName' is not running." } |
This PowerShell script checks for a running process with a specified application name. First, the script defines the application name to search for and retrieves the list of all running processes. It then filters the processes to find one that matches the specified application name. If the process is found, the script calculates the memory usage in megabytes and the total running time, displaying these details along with the application name. If the process is not running, it informs the user that the specified application is not found.
Replace the AppName with the desired app’s name. After the execution of the above script, we will get the usage data of the specified app. The output of the Execute Custom Script can be viewed from the Action History tab of the device.