question

elisorow-6970 avatar image
0 Votes"
elisorow-6970 asked elisorow-6970 answered

powershell sendmail-message date format

Hello All

here a strange thing :

when you're looking for the account expiration date for an AD account with powershell like :

Get-ADUser -identity "user" -Properties * | select Displayname, EmailAddress, Company, Office ,AccountExpirationDate

the result is : AccountExpirationDate : dd-MM-YYYYY 16/06/2021 00:00:00

when i send this information by mail using Sendmail-message :

Body' = "Account of : $($userobj.Displayname) $($userobj.Company)<BR> $($userobj.office) is Expiring on $($userobj.AccountExpirationDate) "


in the mail body AccountExpirationDate is : MM-dd-YYYY : is Expiring on 06/16/2021 00:00:00


devices, servers, MS tenant all are in western EU format : dd-MM-YYYY



is anyone has an idea it will be very helpfull
thank you for your help

BR
Eli

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

In your 1st example you're looking at the string representation of the AccountExpirationDate produced by the default formatting of the Select-Object cmdlet.

In your 2nd example you're seeing the formatting from your Culture settings ((Get-Culture).DateTimeFormat), or the culture of the user running the script.

The AccountExpirationDate object returned by your code is, however, a DateTime object. If formatting consistency is important don't depend on defaults; use Get-Date $userobj.AccountExpirationDate -Format "dd/MM/yyyy"

Note that the AccountExpirationDate always has midnight (00:00:00) as a time, so there's not usually a need to display that.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

elisorow-6970 avatar image
0 Votes"
elisorow-6970 answered

Hello Rich,

your solution is working fine and i thank you
but i also thank you for your explainations there are more important and very helpfull !!

BR
Eli

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.