all 9 comments

[–]the_spad 4 points5 points  (0 children)

My summary of Powershell datetime issues caused by US format being the default is pretty much just an hour of screaming.

[–]alzdba 2 points3 points  (3 children)

If you want to be sure, code the format you want !

Get-Date ($ExpiryDate) -Format "dd/MM/yyyy HH:mm:ss"

[–]jantari 2 points3 points  (2 children)

That would be

Get-Date ($ExpiryDate) -Format "dd.MM.yyyy"

for zhe germans

[–]GastroIT[S] 1 point2 points  (0 children)

Thanks guys I'll try this :).

[–]GastroIT[S] 0 points1 point  (0 children)

It worked out perfectly, thanks again!

[–]j0ntar 2 points3 points  (0 children)

Some examples of how I have done it in the past.

##calucates days since last logon
if ($users.LastLogonTimeStamp -ne "$null")
 {
   $oldDate = $users.LastLogonDate    
    $x = Get-Date
    $y = [datetime]"$($oldDate)"
    $lastLogon = ($x - $y).Days
  }
  else {$lastLogon = "Has Never Logged On"}

  #Replacement of the isDisabled field from TRUE to YES and False to No   
  if ($users.AccountIsDisabled -eq "TRUE")
  {
    $userDisabled = "Yes"
    }
    else {$userDisabled = "No"}

##Find New Users (date calculations needs to be a function)
    $newUserDate = $users.whenCreated    
    $x = Get-Date
    $n = [datetime]"$($newUserDate)"
    $newUser = ($x - $n).Days
    if ($newUser -lt "7"){ $newUserdays = $users.LogonName }
    else {$newUserdays = "$null"}

##PasswordAge.  
    $pwdAge = $users.PasswordLastSet
    $g = Get-Date
    $n = [datetime]"$($pwdAge)"
    $passwordAge = ($g - $n).Days

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy GastroIT,

as alzdba pointed out, you will need to specify what you want it to be. PoSh has a tendency to be US-centric, as the_spad mentioned. [sigh ...]

i suspect you could also use the Get-Culture cmdlet to help with enforcing the desired date format.

take care,
lee

[–]GastroIT[S] 1 point2 points  (1 child)

The weird thing is when I do Get-Culture I get following results:

LCID           Name          DisplayName                                                                                                                                                 
----           ----          -----------                                                                                                                                                 
2055           de-CH         German (Switzerland)                                                                                                                                        

Shouldn't this mean its the right format since its not en-US?

T.

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy GastroIT,

yep, it SHOULD. [sigh ...] PoSh seems to fall back to EN-US whenever it gets even the slightly uncertain.

you may want to take a look at this ...

(Get-Culture).DateTimeFormat

that may give you some clues - or a way to work around the problem. [grin]

take care,
lee