Forum Discussion

tehatchKFI's avatar
tehatchKFI
Copper Contributor
Sep 28, 2023

Get-date AddDays Method Not Returning a Value

I have an unusual one. The AddDays method of Get-Date isn't returning a value on my computer. It's not working in PowerShell 5 or 7. Get-date by itself works find and returns today's date.

 

 

PS C:\Users\Me> get-date

Thursday, September 28, 2023 3:39:06 PM


PS C:\Users\Me> (get-date).AddDays.(-30)
PS C:\Users\Me>

 

 

 

Oddly, it works fine from an elevated window (using a separate admin account).

 

 

PS C:\Scripts> get-date

Thursday, September 28, 2023 3:41:36 PM

PS C:\Scripts> (get-date).AddDays(-30)

Tuesday, August 29, 2023 3:41:49 PM

PS C:\Scripts>

 

 

 

It seems like a profile issue, so I deleted my PS profile and created a new one, but it's the same thing.

 

Anyone seen something like this before?

 

  • tehatchKFI 

     

    In line six of your original post, you have made a small syntax mistake, which is why it returns nothing.

     

    From line 6, this:

     

    (get-date).AddDays.(-30)

     

    Should have been this (note the removal of the period prior to the "(-30)"):

     

    (get-date).AddDays(-30)

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    tehatchKFI 

     

    In line six of your original post, you have made a small syntax mistake, which is why it returns nothing.

     

    From line 6, this:

     

    (get-date).AddDays.(-30)

     

    Should have been this (note the removal of the period prior to the "(-30)"):

     

    (get-date).AddDays(-30)

     

    Cheers,

    Lain

    • tehatchKFI's avatar
      tehatchKFI
      Copper Contributor
      Ok this is wild. It wasn't returning a value. I tried your suggestion, and now it seems to be working fine?

      PowerShell 7.3.7
      PS C:\Users\me> (get-date).AddDays.(-30)
      PS C:\Users\me> (get-date)

      Thursday, September 28, 2023 4:05:39 PM

      PS C:\Users\me> (Get-date).AddDays(-30).ToString('dd-MM-yyyy')
      29-08-2023
      PS C:\Users\me> (get-date).AddDays(10)

      Sunday, October 8, 2023 4:18:14 PM

      PS C:\Users\me> (get-date).AddDays(-10)

      Monday, September 18, 2023 4:18:19 PM

      PS C:\Users\me> (get-date).AddDays(-30)

      Tuesday, August 29, 2023 4:18:23 PM

      PS C:\Users\me>
      • Harm_Veenstra's avatar
        Harm_Veenstra
        MVP
        That is odd... Language/Culture setting, not sure why it's working now.. Even after opening another session?

Resources