Forum Discussion
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?
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
- LainRobertsonSilver Contributor
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
- Ah! It was related to another part of a script, makes sense now 😅
- tehatchKFICopper ContributorDoh! Good catch.
- No, but does (Get-date).AddDays(-30).ToString('dd-MM-yyyy') work?
- tehatchKFICopper ContributorOk 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>- That is odd... Language/Culture setting, not sure why it's working now.. Even after opening another session?