Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

Get Last logon time of a computer Powershell

$
0
0

Hi All,

I've written the below script in powershell to get the both Name and last logon time for computer in a OU

$computers = Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=contoso,DC=local" -Properties lastlogontimestamp

foreach ($computer in $computers){

$PCname = $computer.Name

$Lastlogon = $computer.lastlogontimestamp 

if ($lastlogon.count -eq 0)

{ $time = [DateTime]0 }

else

{ $time = [datetime]$Lastlogon }

}

The problem I have is I want to export the outcome to a CSV in the format Name,Logontime can you please advise how I can change my script to do this thanks


Viewing all articles
Browse latest Browse all 15370

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>