Datum / Uhrzeit

Ein paar kleine Beispiele…

Get-Date
Get-Date -displayhint date
Get-Date -displayhint time

$a = Get-Date "8/1/1972 12:11:10"
$a.DayOfWeek                  # Saturday
Get-Date $a –Format dddd      # Samstag

$Dauer = New-TimeSpan -Days 10 -hours 4 -minutes 3 -seconds 50
$jetzt = Get-Date
$zukunft = $jetzt + $Dauer

Systemzeit setzen mit Set-Date (bei entsprechenden Berechtigungen).

EventLog aus den letzten 12 Stunden

Get-EventLog -LogName System -EntryType Error -After (Get-Date).AddHours(-12)

Was kann Get-Date?

Get-Date | Get-Member -MemberType *Method
# Methode nutzen
(Get-Date).ToShortDateString()

Zeitspanne berechnen

[TimeSpan]5d

DateTime - statische Methoden

[DateTime]::DaysInMonth(2014, 2)
# 28
[DateTime]::DaysInMonth(2016, 2)
# 29

Installzeit (als Unix-Timestamp)

$Path = 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion'
Get-ItemProperty -Path $Path | Select-Object -ExpandProperty InstallDate

Funktionsbeispiel für Umrechnung in Echte Zeit: datetime_cookbook.pdf von Weltner:

function ConvertFrom-UnixTime
 1 function ConvertFrom-UnixTime {
 2     param(
 3         [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
 4         [Int32]
 5         $UnixTime
 6     )
 7     begin {
 8         # $startdate = Get-Date –Date 01.01.1970            # "01/01/1970"
 9         $startdate = Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0
10     }
11     process {
12         $timespan = New-TimeSpan -Seconds $UnixTime
13         startdate + $timespan
14     }
15 }

Jetzt in echter Zeit:

$Path = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion"
Get-ItemProperty -Path $Path | Select-Object -ExpandProperty InstallDate |
    ConvertFrom-UnixTime

Installzeit in vergangenen Tagen

$Path = 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion'
Get-ItemProperty -Path $Path | Select-Object -ExpandProperty InstallDate |
    ConvertFrom-UnixTime | New-TimeSpan | Select-Object -ExpandProperty Days

Zeitzonen

[System.TimeZoneInfo]::GetSystemTimeZones()
[System.TimeZoneInfo]::Local
([System.TimeZoneInfo]::Local).StandardName

Lokalisierte Zeitinfos

[System.Enum]::GetNames([System.DayOfWeek])
# deutsch
0..6 | ForEach-Object { [Globalization.DatetimeFormatInfo]::CurrentInfo.DayNames[$_] }
0..11 | ForEach-Object { [Globalization.DatetimeFormatInfo]::CurrentInfo.MonthNames[$_] }

Verfügbare Culture IDs

[System.Globalization.CultureInfo]::GetCultures(‘InstalledWin32Cultures’)
# Übersetzen der IETF Language Tags
[System.Globalization.CultureInfo]::GetCultureInfoByIetfLanguageTag(‘de-DE’)

Spielerei - Lunch Time für die Titelzeile der Konsole!

function prompt
 1 function prompt {
 2     $lunchtime = Get-Date -Hour 12 -Minute 30
 3     $timespan = New-TimeSpan -End $lunchtime
 4     [Int]$minutes = $timespan.TotalMinutes
 5     switch ($minutes) {
 6         { $_ -lt 0 } { $text = "Lunch is over. {0}"; continue }
 7         { $_ -lt 3 } { $text = "Prepare for lunch! {0}" }
 8     default { $text = "{1} minutes to go... {0}" }
 9     }
10     "PS> "
11     $Host.UI.RawUI.WindowTitle = $text -f (Get-Location), $minutes
12     if ($minutes -lt 3) { [System.Console]::Beep() }
13 }

Noch eine Spielerei mit Uhrzeit in Konsole

function Add-Clock
 1 function Add-Clock {
 2     $code = {
 3     $pattern = "\d{2}:\d{2}:\d{2}"
 4     do {
 5         $clock = Get-Date -Format "HH:mm:ss"
 6         $oldtitle = [system.console]::Title
 7         if ($oldtitle -match $pattern) {
 8             $newtitle = $oldtitle -replace $pattern, $clock
 9             } else {
10                 $newtitle = "$clock $oldtitle"
11                 }
12             [System.Console]::Title = $newtitle
13             Start-Sleep -Seconds 1
14         } while ($true)
15     }
16     $ps = [PowerShell]::Create()
17     $null = $ps.AddScript($code)
18     $ps.BeginInvoke()
19 }
20 
21 # Aufrufen
22 Add-Clock
23 # auch gerne wieder
24 $null = Add-Clock