Powershell new-timespan piping to get-process

Using the command below I get results for some processes and not others.
             new-timespan -start (get-process PROCESSNAME).starttime

For example... It works correctly when I run :
new-timespan -start (get-process explorer).starttime

Days              : 0
Hours             : 3
Minutes           : 6
Seconds           : 25
Milliseconds      : 775
Ticks             : 111857754751
TotalDays         : 0.129464993924769
TotalHours        : 3.10715985419444
TotalMinutes      : 186.429591251667
TotalSeconds      : 11185.7754751
TotalMilliseconds : 11185775.4751

But it doesn't work when I run :
new-timespan -start (get-process chrome).starttime
   New-TimeSpan : Cannot bind parameter 'Start' to the target. Exception setting "Start": "Object reference not set to
   instance of an object."
   At line:1 char:20
   +new-timespan -start <<<<  (get-process chrome).starttime
       + CategoryInfo          : WriteError: (:) [New-TimeSpan], ParameterBindingException
       + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand

The problem I am trying to address is that on our vmware server we get multiple "java" processes that open and then never close when we launch the web client and they each take half a gig of RAM. After a handful of these, the memory gets used up on the server. I want to leverage the command above and say that if the process has been running longer than 24 hours then end it.

Ideas on how to approach this issue. I don't have to use the new-timespan command. It was just the solution I landed on first. I am open to whatever the correct solution is.

Original reference was : http://blogs.technet.com/b/heyscriptingguy/archive/2013/02/27/powertip-use-powershell-to-easily-find-how-long-a-process-runs.aspx
CardlyticsServer TeamAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CardlyticsServer TeamAuthor Commented:
Update : I found a separate powershell that will help me but I'd still like to know why what I was trying didn't work. Please answer my question if possible. This is still a learning experience for me.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Your command only works if exactly one process is found. No process or more than one will result in above error.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:
You just need to turn it around because the problem is likely exactly as Qlemo suggests.

This will show you all long-running processes. I imagine you'll want to put the name back on the process for targetting.
Get-Process  |
  Select-Object Name, ID, StartTime, @{n='RunningFor';e={ New-TimeSpan -Start $_.StartTime }} |
  Where-Object { $_.RunningFor -gt (New-TimeSpan -Days 1) }

Open in new window

Once you have it targetting the right things, and assuming it finds what you want, you can stop processes with this:
Get-Process  |
  Select-Object Name, ID, StartTime, @{n='RunningFor';e={ New-TimeSpan -Start $_.StartTime }} |
  Where-Object { $_.RunningFor -gt (New-TimeSpan -Days 1) } |

Open in new window


David Johnson, CD, MVPOwnerCommented:
$tocheck = "explorer"
try {
    $processes = (get-process $tocheck).StartTime
    foreach ($process in $processes)
        new-timespan -start $process
catch {
write-host("process not found")

Open in new window

CardlyticsServer TeamAuthor Commented:
Thank you for the answers. It got me exactly what I needed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.