Powershell GetDate Method Formatting Question (How can I get it to return MM/DD/YYYY HH:MM:SS)

IT-User9733 71 Reputation points
2020-11-12T17:36:38.87+00:00

Hi I am looking for some assistance with getting this bit of code to work.

I am trying to get back the current date and time in the following format 11/09/2020 00:00:00 using the Date-Time method.

How can I do that?

For example

Where-Object
{
$_.Created -ge "11/09/2020 00:00:00"
}

Thanks

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Bill Stewart 186 Reputation points
    2020-11-13T23:00:47.65+00:00

    You don't need to cast to [DateTime] if you're using Get-Date because it already outputs a [DateTime] object. I'm also not sure why you are calling Get-Date twice.

    All you need is this:

    (Get-Date).AddHours(-24)
    

    or

    (Get-Date).AddDays(-1)
    

    or the equivalent:

    [DateTime]::Now.AddDays(-1)
    
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Bill Stewart 186 Reputation points
    2020-11-12T18:16:33.723+00:00

    Dates in PowerShell are usually [DateTime] .NET objects. It depends on what you want to do and your current language settings.

    Your "for example" isn't too enlightening. Is $_ a file object in your example? In that case it would be

    Where-Object { $_.CreationTime -ge "11/9/2020" }
    

    In my example above for my locale, this would return file objects created on or after November 9 2020.

    You can see what strings you can use right at the PowerShell prompt. Example from my system:

    PS C:\> [DateTime] "11/9/2020"
    Monday, November 9, 2020 12:00:00 AM
    

    Because I didn't add a time stamp, it assumes midnight.

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2020-11-12T19:41:08.797+00:00

    Using string comparisons when dealing with DateTime object hardly ever works the way you think they should. :-)

    Your example would be correct if you cast the string as a DateTime object: Where-Object {$_.CreationTime -ge [DateTime]"11/9/2020"}

    0 comments No comments

  3. Bill Stewart 186 Reputation points
    2020-11-12T19:57:25.867+00:00

    Yes; you can explicitly cast to [DateTime], but in this case the conversion is implicit and works because the left-hand side is a [DateTime].

    Example:

    PS C:\> $dt = [DateTime] "11/9/2020"
    PS C:\> $dt -eq "2020/11/09 12:00am"
    True
    

    Note how the right-hand side of the comparison is a string (even with a different date format and including the time); because the left hand side is a [DateTime], PowerShell casts the right-hand side to [DateTime] and the comparison returns true.


  4. Anonymous
    2020-11-13T02:59:09.373+00:00

    Hi,

    Get-Date rerurns a System.DateTime object. You can format it to a string like Get-Date -format "MM/dd/yyyy HH:mm:ss". But if you want to compare the dates, it's unnecessary to format it as you can only compare dates of type DateTime.

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.