1

I like to have the following PowerShell script output without the date part from the table.


10/21/2017 16:49 | Systrax Restore Point

Currently the output is:

@{Date=10/21/2017 12:40} | Systrax Auto Restore Point

This is part of my script:

$creationtime = Get-ComputerRestorePoint | Select-Object @{Label="Date"; Expression={"{0:MM/dd/yyyy HH:mm}" -f $_.ConvertToDateTime($_.CreationTime)}}
$restorepoint = (Get-ComputerRestorePoint).Description
if ($restorepoint -eq $null){
    Enable-ComputerRestore -Drive "C:\"
    Checkpoint-Computer "Systrax Auto Restore Point"
    Write-Host "No restore points, Auto Creating..."
    Exit 1010
}
else {
    Write-Host "$creationtime | $restorepoint"
    Exit 0
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
IIIdefconIII
  • 353
  • 2
  • 5
  • 13
  • Possible duplicate of [Given a DateTime object, how do I get an ISO 8601 date in string format?](https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format) – Tomalak Oct 21 '17 at 10:29
  • Another duplicate. https://stackoverflow.com/questions/2249619/how-to-format-a-datetime-in-powershell, among many many others. Please search before asking. Relevant documentation. https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings (Besides, `mm/dd/yyyy` is not a sensible date format, avoid.) – Tomalak Oct 21 '17 at 10:30
  • got it (Get-ComputerRestorePoint).CreationTime, (Get-Date).ToString('yyyy-MM-dd') But then its still outputting the original timeformat 20171021104047.441419-000 2017-10-21 – IIIdefconIII Oct 21 '17 at 10:47
  • im a total noob and just looking for someone who might wanne make this script correct for me, willing to do some donation – IIIdefconIII Oct 21 '17 at 10:53
  • Please see my [answer on Date-Time formatting](https://stackoverflow.com/a/51898787/5571827). HTH. – Eddie Kumar Aug 21 '18 at 09:59

1 Answers1

0

You can use the -f format operator to format the resulting DateTime object in your calculated property:

... |Format-Table @{Label="Date"; Expression={"{0:MM/dd/yyyy HH:mm}" -f $_.ConvertToDateTime($_.CreationTime)}}

Another option is calling ToString() on the object:

... |Format-Table @{Label="Date"; Expression={$_.ConvertToDateTime($_.CreationTime).ToString("MM/dd/yyyy HH:mm")}}

Or you can have Get-Date do it for you:

... |Format-Table @{Label="Date"; Expression={$_.ConvertToDateTime($_.CreationTime) |Get-Date -Format "MM/dd/yyyy HH:mm"}}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thank you but it didnt worked: still getting the time in unreadable format 20171021104047.441419-000 `` `(Get-ComputerRestorePoint).CreationTime | Format-Table @{Label="Date"; Expression={"{0:MM/dd/yyyy HH:mm}" -f $_.ConvertToDateTime($_.CreationTime)}}` – IIIdefconIII Oct 21 '17 at 11:52
  • `Get-ComputerRestorePoint | Format-Table ...`, not `(Get-ComputerRestorePoint).CreationTime | ...` – Mathias R. Jessen Oct 21 '17 at 12:39
  • ill be damned, cool this cause the info is in the table, got it, but i still have a table and i want the outplain plain is this even possible? ty – IIIdefconIII Oct 21 '17 at 17:09
  • Replace `Format-Table` with `Select-Object` if you want to work with the resulting object further – Mathias R. Jessen Oct 21 '17 at 17:10
  • maybe something with write-host, select second/thirs row from previous output, is this possible? if you look at the OP you might get an idea what i really want – IIIdefconIII Oct 21 '17 at 20:30