How to export the last login details of all users in a Windows server using Powershell

So one of our clients want to get a monthly report on the last login details of all users in the Windows servers in our environment. So we came up with this powershell script which is scheduled to run end of every month. This will extract the data, and upload them to an S3 bucket. It makes use of awscli for uploading to s3. We can even include SNS notification, but right now it is not implemented.

Below is the script:

Disclaimer : I do not know if this is the best way to do it just like all my other scripts, but this works [ At least for me ]

$currentMonth = Get-Date -Format MM
$currentYear = Get-Date -UFormat %Y
$hostname = hostname
$filename = $currentYear+""+$currentMonth+""+$hostname+"_login.csv"
$([ADSI]"WinNT://$env:COMPUTERNAME").Children | where {$_.SchemaClassName -eq 'user'} | select @{l='name';e={$_.name}},@{l='LastLogin';e={$_.lastlogin}} | export-csv C:/temp/$filename

(gc C:/temp/$filename) -replace (gc C:/temp/$filename)[0],"" | sc C:/temp/$filename -Force
(gc C:/temp/$filename) -replace (gc C:/temp/$filename)[1],"" | sc C:/temp/$filename -Force
(gc C:/temp/$filename) | ? {$_.trim() -ne "" } | set-content C:/temp/$filename

aws s3 cp C:/temp/$filename s3://YourBucket/$currentYear/$currentMonth/

This will create a csv file in the following format.
windows_last_login

How I managed to get all 3 AWS associate certifications..

You read it right.
Today I got my third AWS certification – AWS certified SysOps administrator.

I think a lot of it comes from common sense, and some general industry knowledge. I only have less than 2 years of IT experience, and even in that 1.5 years was of just desktop support. Its from my current job that I really started some ‘real IT’ job. But for past few years I have been following major tech sites, and so called geeky facebook pages and the famous r/sysadmin. I think all those things gave me some idea on what is happening in the industry although I don’t really have any experience.

Coming to AWS, I first heard about AWS back in 2015 when I was starting to look for an IT job. But then..nothing happened. So its only since my current job, precisely from May 2017, that I really started doing AWS stuffs. That is around 6 months now. In this 6 months, I haven’t really implemented anything from scratch, but just have been maintaining the existing stuffs.

One good thing about my employer is that they value these certifications, and will reward if we get one, and also will refund the exam fee. So, why not? Basically my preparation was same. I bought the courses from A Cloud Guru, watched them all. Once done with that, bought practice exams from Whizlabs. Other than these two, I referred the AWS FAQs.That’s it. As I mentioned earlier, I think its all about common sense and general knowledge about IT and AWS. The exams are not really difficult. Especially the SA and Dev. I got 89% for SA, 96% for Dev and 87% for SysOps.

Why am I taking all these certs ? Well I think its nice to put this up in resume and LinkedIn. And it doesnt cost me anything. So why not?