-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-MyLocalLogonTime.ps1
54 lines (47 loc) · 1.89 KB
/
Get-MyLocalLogonTime.ps1
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
function Get-MyLocalLogonTime {
<#
.SYNOPSIS
Gets local logon time for the current user
.DESCRIPTION
Gets local logon time for the current user
.PARAMETER Detail
Switch to include detail in output. If not specified just the logon time is returned.
.EXAMPLE
Get-MyLocalLogonTime
Friday, July 30, 2021 7:54:36 AM
.EXAMPLE
Get-MyLocalLogonTime -Detail
User ComputerName LocalLogonTime
---- ------------ --------------
contosco\testuser demolaptop 7/30/2021 7:54:36 AM
.NOTES
Inspired by: https://community.idera.com/database-tools/powershell/script_library/m/local-accounts/30450
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseCmdletCorrectly', '')]
param (
[switch] $Detail
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
$RegKey = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$([Security.Principal.WindowsIdentity]::GetCurrent().User.Value)"
Write-Verbose -Message "Getting logon time information from [$RegKey]"
$Raw = Get-ItemProperty -Path $RegKey -Verbose:$false
Write-Verbose -Message "Converting logon time values to datetime"
$LocalLogonTime = ConvertTo-DateTime -DateString ([uint64] ($Raw.LocalProfileLoadTimeHigh * (Get-Power -Base 2 -Power 32)) + $Raw.LocalProfileLoadTimeLow) -FileTime -Verbose:$false
if (-not $Detail) {
$LocalLogonTime
} else {
New-Object -TypeName psobject -Property ([ordered] @{
User = ([Environment]::UserDomainName + '\' + [Environment]::UserName)
ComputerName = $env:COMPUTERNAME
LocalLogonTime = $LocalLogonTime
})
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}