none
powershell filter a string RRS feed

  • Question

  • Hi

    i am new to powershell.

    I am getting the output from the cmdlet

    $a = klist tgt 
    $b = $a -match "StartTime          : " 
    $b

    StartTime          : 12/13/2015 17:49:01 (local)

    I just need the time. I am searching for on how to remove the other characters from the output. If you have any idea just let me know.



    Regards Sushain KApoor

    Sunday, December 13, 2015 6:31 PM

Answers

  • And how about:

    $b.split(" ")[12]
    :)

    You can remove the starting text with the trimstart method.

    • Marked as answer by Sushain_Kapoor Sunday, December 13, 2015 8:00 PM
    Sunday, December 13, 2015 7:03 PM

All replies

  • Can't test right know with a Kerberos ticket, but if $b has a datetime data type, then you could use the same logic as with get-date:

    (get-date).ToLongTimeString()

    Sunday, December 13, 2015 6:42 PM
  • not working, Just wanted to check if can i store output of $b in $c and remove the text ahead with del command or filter the format

    Regards Sushain KApoor

    Sunday, December 13, 2015 6:53 PM
  • And how about:

    $b.split(" ")[12]
    :)

    You can remove the starting text with the trimstart method.

    • Marked as answer by Sushain_Kapoor Sunday, December 13, 2015 8:00 PM
    Sunday, December 13, 2015 7:03 PM
  • thanks buddy the desired output is visible. but when i see the overall purpose $d is string and its not getting converted to dat time format so actal substraction of time difference is still incorrect 

    $a = klist tgt 
    $b = $a -match "StartTime          :" 
    $c = $b.trim("StartTime          :")
    $d = $c.Trim(" (local)")
    $d
    $details2 = Get-Date
    $details2
    $time = $details2.TimeOfDay.TotalSeconds - $t.TimeOfDay.TotalSeconds
    $time 


    Regards Sushain KApoor

    Sunday, December 13, 2015 7:48 PM
  • You only need one line to do it all:

    PS>$dt=if('StartTime          : 12/13/2015 17:49:01 (local)' -match ' (\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2})'){[datetime]$matches[1]}
    PS>$dt

    Sunday, December 13, 2015 5:49:01 PM

    To calculate we do this:
    ($dt - [datetime]::Now).TotalSeconds

    YOU should lookup the documentation for you objects to see now they work.


    \_(ツ)_/


    • Edited by jrv Sunday, December 13, 2015 8:14 PM
    Sunday, December 13, 2015 8:10 PM