Skip to content

Instantly share code, notes, and snippets.

@thespad
Last active December 16, 2017 14:35
Show Gist options
  • Save thespad/dcf647cb83b38f107365eb26613848e5 to your computer and use it in GitHub Desktop.
Save thespad/dcf647cb83b38f107365eb26613848e5 to your computer and use it in GitHub Desktop.
Recording Game Run Time
[CmdletBinding()]
param()
#Modified from original script https://herringsfishbait.com/2015/03/06/powershell-recording-process-run-time-or-how-long-have-i-played-that-game-for/
<# THESE ARE THE BITS YOU CHANGE #>
# Where the Output File Goes
[string]$DataFilePath="$env:userprofile\Documents\GamePlayTimes.csv"
# Which paths to include
[Array]$Filters="D:\Games","D:\Steam\steamapps","D:\Origin Games","E:\","F:\steam\steamapps"
# Which executables to exclude (without the file extension)
[Array]$Exclude="Battle.net","Battle.net Helper"
# Time to pause between discovery cycles
$sleep = 30
<# YOU DON'T CHANGE OTHER BITS #>
function Get-MatchingProcess{
param
(
[System.Array]$DataArray,
[parameter(Mandatory=$True)]
[System.Object]$Item
)
$DataArray | ? {($_.Path -eq $Item.Path) -and ((get-date -date $_.Start -Millisecond 0) -eq (get-date -date $Item.Start -Millisecond 0))} | Select-Object -First 1
}
Write-Verbose "Process Search String : $Filters"
Write-Verbose "Data File Location : $DataFilePath"
while($true){
Write-Verbose ("Running cycle.")
if (Test-Path $DataFilePath){
[System.Array]$Data=Import-Csv -Delimiter ',' -LiteralPath $DataFilePath
}else{
$Data=@()
}
$Processes = get-process
foreach($Filter in $Filters){
[Array]$FilteredProcesses += $processes | ? {($_.Path -like "*$filter*") -and ($_.ProcessName -notin $Exclude)} | select Path,@{name="Start";expression={$_.StartTime}},@{name="RunningTimeInSeconds";expression={(New-TimeSpan -Start ($_.StartTime)).TotalSeconds}}
}
Foreach ($Process in $FilteredProcesses){
$Result=Get-MatchingProcess $Data $Process
if ($Result){
Write-Verbose "Modifying Existing Record."
$Result.RunningTimeInSeconds=(New-TimeSpan -Start $Process.Start).TotalSeconds
}else{
Write-Verbose "Adding New Record."
$Data=$Data+$Process
}
}
$Data | Export-Csv -Path $DataFilePath -NoTypeInformation
sleep $sleep
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment