Export AD Users into CSV with lastlogon date more than 180 days

Abdulrahman 101 Reputation points
2022-08-29T19:31:23.973+00:00

Hi everyone,

I want to export all of my AD users whom have not logged in more than 180 days.

Is there anyway I can do it through Powershell or directly from ADUC?

Thank you..

Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
{count} votes

4 answers

Sort by: Most helpful
  1. JimmySalian-2011 42,491 Reputation points
    2022-08-29T19:47:15.153+00:00

    Hi,

    Yes copy paste in the powershell and yes try with admin rights to launch powershell.

    ==
    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    1 person found this answer helpful.

  2. JimmySalian-2011 42,491 Reputation points
    2022-08-30T08:41:52.047+00:00

    Hi,

    I have updated the code via the Q&A feature this should work without any spaces or hypen. Thanks @Andreas Baumgarten

    Import-Module ActiveDirectory  
      
    $DaysInactive = 180  
    $Time = (Get-Date).Adddays( - ($DaysInactive))  
      
    Get-ADUser -Filter { LastLogonTimeStamp -lt $Time -and enabled -eq $true } -Properties * |  
    Select-Object Name, LastLogonDate |  
      
    Export-Csv "c:\Export\InactiveUsers.csv" -Encoding UTF8 -NoTypeInformation  
    

    ==
    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    1 person found this answer helpful.
    0 comments No comments

  3. JimmySalian-2011 42,491 Reputation points
    2022-08-29T19:37:03.65+00:00

    Hi Abdul,

    Yes you can use this script:

    Import-Module ActiveDirectory

    $DaysInactive = 180
    $Time = (Get-Date).Adddays( - ($DaysInactive))

    Get-ADUser -Filter { LastLogonTimeStamp -lt $Time -and enabled -eq $true } -Properties * |
    Select-Object Name, LastLogonDate |

    Export-Csv "c:\Export\InactiveUsersdate.csv" -Encoding UTF8 -NoTypeInformation

    =
    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


  4. JimmySalian-2011 42,491 Reputation points
    2022-08-29T20:05:05.007+00:00

    Hi,

    It seems you copied and paste the command there might be some space or characters in the script, just make sure not to put any space between the hyphen(-) and the Name parameter and the same way Don’t put any space between the hyphen(-) and Location parameter.

    ==
    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.