0

In Windows, using PowerShell or cmd shell, how can I get the list of all currently running applications sorted by the time they were lastly used (i.e. they were in the foreground)? To be more specific, I need PIDs and process names for those applications.

Ideally, I want the list to be the same as the apps displayed in the Windows Taskbar, that is, including File explorer.

I need this list to implement an application and windows switcher utility in Python. So far, I've ound a gps or get-process command in PowerShell solution, but with that solution, I have not found the way to sort the listing by the time the apps were lastly used. Also, it list some extra items than just the apps one can find in the Windows Taskbar, and for some apps, such as cmd shell windows, it lists every instance of that app window as a separate item in the listing, whereas I need the list to include only the cmd window which was used most recently.

Therefore, in the first step, I need this list of running applications, and then based on that I would like to create a submenu for every application item in the list to include all the windows pertaining only to the selected application, which the user will be abel to switch to.

4

1 Answer 1

1

As per @ReddyLutonadio', you'll get:

Get-Process | 
Where {$_.MainWindowTitle } | 
ForEach { Get-ChildItem $_.Path} | 
Select Name, LastAccessTime | 
Sort LastAccessTime
# Results
<#
Name                     LastAccessTime
----                     --------------
TextInputHost.exe        24-Jul-22 18:48:48
Calculator.exe           24-Jul-22 18:49:11
SystemSettings.exe       24-Jul-22 18:55:28
msedge.exe               24-Jul-22 21:53:50
OUTLOOK.EXE              24-Jul-22 21:53:50
EXCEL.EXE                24-Jul-22 21:53:50
ApplicationFrameHost.exe 24-Jul-22 21:53:50
powershell.exe           24-Jul-22 21:53:50 <--------- Process that ran the command
#>

Refactoring '@ReddyLutonadio' helpful answer, as indicated by 'DrMoishe Pippik' comment, to show what I mean

Eliminate the current process which ran the command.

(Get-Process | 
Where { $_.MainWindowTitle } | 
ForEach { Get-ChildItem $_.Path} | 
Select Name, LastAccessTime | 
Sort LastAccessTime) -notmatch [System.Diagnostics.Process]::GetCurrentProcess()

# Results
<#
Name                     LastAccessTime
----                     --------------
TextInputHost.exe        24-Jul-22 18:48:48
Calculator.exe           24-Jul-22 18:49:11
SystemSettings.exe       24-Jul-22 18:55:28
msedge.exe               24-Jul-22 21:54:50
OUTLOOK.EXE              24-Jul-22 21:54:50
EXCEL.EXE                24-Jul-22 21:54:50
powershell.exe           24-Jul-22 21:54:50
ApplicationFrameHost.exe 24-Jul-22 21:54:50
#>

Point of note:

The above response only works when executed from a true PowerShell console instance. It does not work in the PowerShell ISE.

Note:

When using the 'Windows terminal' PScore (psv7...) that filter is not needed as the process is not reported back. You also will get a much longer list.

Note:

In raw PSCore, you still need the filter as shown.

So, the point is, that you need to keep in mind where you are running this in mind as the results will differ.

So, test the command in powershell.exe, powershell_ise.exe, pwsh.exe, and from the 'Windows Terminal', via 'Windows PowerShell and PowerShell Core' to see for yourself what the results will be.

Also, test in VSCode, just to be sure you know what is what.

Update and per your question

Get-Process | 
Where { $PSItem.MainWindowTitle } | 
ForEach-Object { $PSItem } | 
Select ProcessName, Id, 
@{Name = 'LastAccessTime';Expression = {(Get-ChildItem $PSItem.Path).LastAccessTime}} | 
Sort LastAccessTime
# Results
<#
ProcessName             Id LastAccessTime
-----------             -- --------------
TextInputHost        14048 25-Jul-22 08:06:59
SystemSettings        7468 25-Jul-22 08:07:18
Calculator            8824 25-Jul-22 08:08:01
msedge               11372 25-Jul-22 10:40:10
OUTLOOK              10988 25-Jul-22 10:40:10
powershell_ise       20524 25-Jul-22 10:40:10
ApplicationFrameHost  7496 25-Jul-22 10:40:10
notepad               4212 25-Jul-22 10:40:10
powershell           17824 25-Jul-22 10:40:11
powershell           21824 25-Jul-22 10:40:11
#>
3
  • Thanks. But I don't need the Name column but rather the ProcessName and Id columns. How can I achieve that? I tried Get-Process | Where { $_.MainWindowTitle } | ForEach { Get-ChildItem $_.Path} | Select ProcessName, Id, LastAccessTime | Sort LastAccessTime but that returns only the LastAccessTime column.
    – Adam
    Jul 25, 2022 at 13:42
  • It's not there because you did not really ask for it from the source. See my update for you.
    – postanote
    Jul 25, 2022 at 17:40
  • Sorry, I've updated the question. However, the updated command you posted does not return anything on my system.
    – Adam
    Jul 27, 2022 at 14:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .