Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Poweshell alias for startTIME of Process

I am trying to create a simple powershell script which will allow it to display the PID and the starttime of a process.

For the moment this is my code –>

$proc=$args[0]
    if (get-process -name $proc -ErrorAction SilentlyContinue) { 
    $time= get-process $proc | Select-Object starttime |format-table -HideTableHeaders starttime 
    write-host  $proc $time} 
    else {write-host "non existant"}

When i look at the content of the variable $time , this is what is stored

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
> Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Micr
> osoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.Powe rShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Com mands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Inte rnal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Forma t.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEn tryData
> Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
> Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Micro
> soft.PowerShell.Commands.Internal.Format.FormatEndData

Basicaly , what i would like to end with is a script that shows every PID and Startime for a particular application. The $time var would need to store the date "2022-08-18 1:08:39 PM"

I would also create another variable called $id which would store the PID of each process.

1408 notepad Started 2022-08-18 1:08:39 PM

Or for applications with multiple processes

1409 chrome Started 2022-08-18 1:08:39 PM
14264 chrome Started 2022-08-18 1:08:40 PM
4567 chrome Started 202...

For the ones that have multiple processes , I guess I would use an foreach statement and go through every PID available in the variable ? Still new at this so if you have any advice on how to go about it would be appreciated , but in regards to what I know , a foreach statement would help me with this.

>Solution :

There are two common problems with your code:

  • Format-* cmdlets emit output objects whose sole purpose is to provide formatting instructions to PowerShell’s for-display output-formatting system. In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing – see this answer for more information.

  • The reason you tried to use Format-Table to begin with was in order to get just the .StartTime property value. The proper way to get Select-Object to output just a property value is to use the -ExpandProperty parameter, i.e.
    -ExpandProperty starttime instead of [-Property] starrttime.


A PowerShell-idiomatic solution would look something like this:

$proc = $args[0]

# Process all matching processes, if any.
Get-Process $proc -ErrorAction SilentlyContinue |
  ForEach-Object {
    '{0} {1} Started {2}' -f $_.Id, $_.Name, $_.StartTime
  }

# $? contains $false if an error occurred in the previous statement.
if (-not $?) { Write-Warning "Non-existent: $proc" }

That is, matching processes are processed one by one using ForEach-Object, and in the script block ({ ... }) passed to it you can refer to the process-info object (System.Diagnostics.Process) at hand via the automatic $_ variable variable, allowing you to access the properties of interest directly on that object. The -f operator is used to synthesize the output strings in the desired format.

Sample output:

56204 bash Started 8/18/2022 2:15:25 PM
56245 bash Started 8/18/2022 2:15:28 PM
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading