Script for finding servers that are not logged on

Welcome Forums General PowerShell Q&A Script for finding servers that are not logged on

This topic contains 2 replies, has 3 voices, and was last updated by

2 years, 2 months ago.

  • Author
  • #60937

    Points: 0
    Rank: Member

    Hello! Could someone please help me with a script for finding servers that has not been accessed during the last 6 months?
    I must be able to exclude service users. There are so many servers in my company so we will figure this out. Appreciate any help.

  • #60939

    Points: 24
    Team Member
    Rank: Member

    Using the Search-ADAccount cmdlets which is part of the Active Directory PowerSHell module installed via the Remote Server Administration Tools you can discover inactive computer and user accounts.

    Import-Module ActiveDirectory
    # Generic call using defaults
    Search-ADAccount -AccountInactive -ComputersOnly
    # All computer accounts that have no activity for the last 365 days
    Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 365.00:00:00


  • #60940

    Points: 1
    Rank: Member

    Not sure if this is what you're after, but if you're looking for interactive logons by users, as opposed to authentications by service accounts you could use the user profile information. Assuming you have a list of servers to loop through you could do something like this to get the age of the most recently accessed user profile. Of course this doesn't take into account active and valid servers doing server stuff with no user logons, but it's something:

    $Scriptblock = {
        $lastaccessdate = Get-CimInstance win32_userprofile | sort-object -property lastusetime -descending | select -first 1 | select -expandproperty lastusetime
        $ServerAccessAge = $(get-date) - $lastaccessdate
        write-output $ServerAccessAge
    $servercred = get-credential
    $serverlist = get-content c:\scripts\serverlist.txt
    foreach ($server in $Serverlist){
        $ServerAccessAge = invoke-command -ComputerName $Server -Credential $servercred -ScriptBlock $Scriptblock
        if ($ServerAccessAge.days -gt 90){
            new-object psobject -Property @{
                Server = $Server
                Age = $ServerAccessAge


The topic ‘Script for finding servers that are not logged on’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort