title: Get list of users from file for powershell loop
body: <p>I'm trying to modify this PowerShell script to allow input of users from a text or CSV file. I've pulled the script directly from <a href="https://interworks.com/blog/trhymer/2014/01/22/powershell-get-last-logon-all-users-across-all-domain-controllers/" rel="nofollow noreferrer">Tim Rhymer</a>. </p>
  
  <p>This is the original script:</p>
  
  <pre><code>Import-Module ActiveDirectory
  
  function Get-ADUsersLastLogon() {
    $dcs = Get-ADDomainController -Filter {Name -like "*"}
    $users = Get-ADUser -Filter *
    $time = 0
    $exportFilePath = "c:lastLogon.csv"
    $columns = "name,username,datetime"
  
    Out-File -FilePath $exportFilePath -Force -InputObject $columns
  
    foreach ($user in $users) {
      foreach ($dc in $dcs) { 
        $hostname = $dc.HostName
        $currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon
  
        if ($currentUser.LastLogon -gt $time) {
          $time = $currentUser.LastLogon
        }
      }
  
      $dt = [DateTime]::FromFileTime($time)
      $row = $user.Name + "," + $user.SamAccountName + "," + $dt
  
      Out-File -FilePath $exportFilePath -Append -NoClobber -InputObject $row
  
      $time = 0
    }
  }
  
  Get-ADUsersLastLogon
  </code></pre>
  
  <p>I'm thinking I should modify the portion of the script the sets the variable of <code>$user</code> and how the <code>foreach ($user in $users)</code> loop processes each user, but can't figure it out.</p>