-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteLastLogonDate.ps1
33 lines (27 loc) · 1.22 KB
/
WriteLastLogonDate.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
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear();
#Test and Set Username from Environment Variable
$Username = $ENV:USERNAME
If (!$Username) {
$Username = [Environment]::UserName
}
#The below checks for the user profile path
$testUserProfilepath = Test-Path -Type Container "C:\Users\$($Username)"
#If the path exists then it will write the date to the file
If ($testUserProfilepath -eq $true)
{
$NTUSERDat = Get-Item "C:\Users\$($Username)\NTUSER.DAT" -Force
$NTUSERDatLastModified = $NTUSERDat.LastWriteTime
$UsrClassDat = Get-Item "C:\Users\$($Username)\AppData\Local\Microsoft\Windows\UsrClass.dat" -force
"$(Get-Date -Date $USRClassDat.LastWriteTime -uformat "%Y/%m/%d")" | Out-File -FilePath "C:\Users\$($Username)\LastLogonDate.txt" -Encoding ascii
$UsrClassDatLastModified = $UsrClassDat.LastWriteTime
if ($NTUSERDatLastModified -ne $UsrClassDatLastModified)
{
$NTUSERDat.LastWriteTime = $UsrClassDatLastModified
Write-Host "$($Username) - NTUSER.dat Last Modified date updated to match UsrClass.dat"
}
else
{
Write-Host "$($Username) - NTUSER.dat Last Modified date already matches UsrClass.dat"
}
}