Get users who did not login to exchange server for the past 120 days using powershell script in C# RRS feed

  • Question

  • I have an web application that needs to connect to Exchange server and get list of users who did not login to the mailbox in the last 120 days. 

    This needs to be done using powershell script in C#. 
    The code uses powershell script to find the users who have not logged in to exchange server in 120 days. When trying to use the script mentioned in the code it returns count as 0 but there are many users who have not logged in the past 120 days.

    Below the current code.
        using System;
        using System.Web.UI;
        using System.Web.UI.HtmlControls;
        using System.Security;
        using System.Management.Automation;
        using System.Management.Automation.Runspaces;
        using System.Collections.ObjectModel;
        using System.DirectoryServices;
        namespace Exchange
            public partial class NoLoginLast120Days : System.Web.UI.Page
                protected void Page_Load(object sender, EventArgs e)
                    string serverAddress = "my server address", runasPassword = "serverpassword", runasUsername = "server username";
                    // Prepare the credentials that will be used when connecting
                    // to the server. More info on the user to use on the notes
                    // below this code snippet.
                    SecureString ssRunasPassword = new SecureString();
                    foreach (char x in runasPassword)
                    PSCredential credentials = new PSCredential(runasUsername, ssRunasPassword);
                    // Prepare the connection
                    var connInfo = new WSManConnectionInfo(
                        new Uri("https://" + serverAddress + "/PowerShell"),
                    connInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
                    connInfo.SkipCACheck = true;
                    connInfo.SkipCNCheck = true;
                    // Create the runspace where the command will be executed
                    var runspace = RunspaceFactory.CreateRunspace(connInfo);
                    PowerShell ps = PowerShell.Create();
                    ps.Runspace = runspace;
                    Command getStatistics = new Command("Get-MailboxStatistics");
                    getStatistics.Parameters.Add("Filter", "$_.Lastlogontime -lt (get-date).AddDays(-1000)");            
                    Collection<PSObject> commandResults = ps.Invoke();

    I tried getStatistics.Parameters.Add("Filter", "Lastlogontime $null");         for getting results which had lastlogontime as null, it was successful.
    I was able to process the commandResults. 

    I even tried the below for getting users with lastlogontime as not null, so I can filter the desired results using C# but it did not work out. 

    getStatistics.Parameters.Add("Filter", "Lastlogontime -ne $null");

    Also the users count is huge that I get System.OutOfMemoryException if I try without any filters.

    If there is a way to get users who have not logged in for the past 120 days using powershell script in C# it will be really helpful as I am stuck with this for a long time.

    Below code works in powershell. It will be really helpful to know how to use this in C#.

        Get-MailboxStatistics -Server <servername> | where {$_.LastLogonTime -lt (get-date).AddDays(-120)} | ft displayName, lastlogontime
    Thursday, August 17, 2017 8:24 PM

All replies

  • What makes you think you need to use C# to do this?

    Just use PowerShell directly.


    Thursday, August 17, 2017 8:55 PM
  • Filter syntax is wrong.  Do it in PowerShell until you learn the filter syntax.

     @"Lastlogontime -lt (get-date).AddDays(-1000)"           


    Thursday, August 17, 2017 9:01 PM
  • @jrv : -1000 ? Shouldn't be -120
    Thursday, August 17, 2017 9:07 PM
  • I just copied your code.


    Thursday, August 17, 2017 9:11 PM