Get-Date adding in a line before outputting

Welcome Forums General PowerShell Q&A Get-Date adding in a line before outputting

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5245

    Points: 0
    Rank: Member

    by jawhitm at 2012-10-30 02:34:46

    Hello everyone,

    I am recording the information from Get-Date to a text file because I am going to compare it against events that occur in the future. As well I do not want it to alert again within a specified time range.

    My issue is as follows:

    1. If you use Get-Date it adds in a line above it.

    2. This is extremely annoying as when I do a Get-Date > C:\temp\CurrentTime.txt

    3. Then do a [datetime] $RecordedTime = Get-content C:\temp\CurrentTime.txt

    It then errors out telling me that it cannot convert it. Which is perfectly true as well a date and time of course cannot be blank.

    Any suggestions on how I can use Get-date but not have the new line appear. And no Write-host Get-Date -NoNewLine does not work. 🙂

    Thank you for your time.

    Thank you.

    by Klaas at 2012-10-30 03:04:29

    That's because get-date returns an object, not a string. You can pass -format or -displayhint parameters or use a .tostring() method to pull the exact output you want.

    by mikefrobbins at 2012-10-30 10:18:18

    Use one of these two for recording the DateTime into the text file:

    (Get-Date).datetime | Out-File C:\temp\CurrentTime.txt


    Get-Date | Select-Object -ExpandProperty DateTime | Out-File C:\temp\CurrentTime.txt

    Then your other command will work without error:

    [datetime] $RecordedTime = Get-content C:\temp\CurrentTime.txt

    by jawhitm at 2012-10-30 14:07:56

    mikefrobbins (Get-Date).datetime worked exactly what I was looking for

The topic ‘Get-Date adding in a line before outputting’ is closed to new replies.