-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-CurrentDateTime.ps1
More file actions
60 lines (49 loc) · 1.69 KB
/
Copy pathGet-CurrentDateTime.ps1
File metadata and controls
60 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function Get-CurrentDateTime {
<#
.SYNOPSIS
Returns the current date and time in a specified format.
.DESCRIPTION
Returns the current date and time formatted according to the specified format string.
Supports common format presets or custom format strings.
.EXAMPLE
Get-CurrentDateTime
Returns the current date and time in the default format (yyyy-MM-dd HH:mm:ss).
.EXAMPLE
Get-CurrentDateTime -Format 'Short'
Returns the current date in short date format.
.EXAMPLE
Get-CurrentDateTime -Format 'Custom' -CustomFormat 'dddd, MMMM dd, yyyy'
Returns the current date in a custom format like "Monday, January 20, 2026".
.LINK
https://MariusStorhaug.github.io/MariusTestModule/Functions/DateAndTime/Get-CurrentDateTime/
#>
[OutputType([string])]
[CmdletBinding()]
param (
# The format preset to use for the date and time output.
[Parameter()]
[ValidateSet('Default', 'Short', 'Long', 'ISO8601', 'Custom')]
[string] $Format = 'Default',
# Custom format string when Format is set to 'Custom'.
[Parameter()]
[string] $CustomFormat = 'yyyy-MM-dd HH:mm:ss'
)
$currentDateTime = Get-Date
switch ($Format) {
'Default' {
$currentDateTime.ToString('yyyy-MM-dd HH:mm:ss')
}
'Short' {
$currentDateTime.ToShortDateString()
}
'Long' {
$currentDateTime.ToLongDateString()
}
'ISO8601' {
$currentDateTime.ToString('o')
}
'Custom' {
$currentDateTime.ToString($CustomFormat)
}
}
}