title: pull one line at a time from text file to use as a variable in powershell
body: <p>I am trying to write a PowerShell version 5 script that will query a text document, that simply has a list of usernames, and then run a series of <code>Get-ADUser</code> and <code>Set-ADUser</code> commands against each one.</p>
  
  <p>I have the script working so that if I enter a single username (<code>$SamAccountName</code> is the variable that I use now for the <code>-Identitity</code> modifiers) it works great, but now I want it to run batches from a TXT file.</p>
  
  <pre><code>#Pull a list of users from Text file
  $TXTfile = Read-Host -Prompt 'Enter path to Text File'
  $file = Get-Content $TXTfile
  $file | foreach {
    $items = $_.Split("=")
    if ($items[0] -eq "") { $SamAccountName = $items[1] }
  }
  
  echo $SamAccountName
  </code></pre>
  
  <p>EDIT: I pulled this code from the web and tried to make it work, but it may be the wrong code, more than likely I'm missing some brackets - what can I say I'm a nube.</p>
  
  <p>The error that I get is: </p>
  
  <blockquote>
    <p>Enable-ADAccount : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.</p>
  </blockquote>
  
  <p>My text file simply looks like this:</p>
  
  <pre><code>SmithA
  TurnerH
  SchmoJ
  TrumpD
  </code></pre>
  
  <p>Here is the full script that I run for disabling individual accounts:</p>
  
  <pre><code># this Powershell script will disable a users stored in a TXT file.
  # along with disabling their account it will also: 
  # -Strip thier Group memberships
  # -Update some attributes
  # -Move the account to the Disabled User's OU
  #
  #
  $UC = Get-Credential
  $Date = Get-Date
  $Ticket = Read-Host -Prompt 'Input LANDesk ticket number'
  #
  #
  #Prompt for to enter a single username:#
  #$samAccountName = Read-Host -Prompt 'Input Username to be disabled:'
  #
  #
  
  #Pull a list of users from Text file
  $TXTfile = Read-Host -Prompt 'Enter path to Text File'
  $file = Get-Content $TXTfile
  $file | foreach {
    $items = $_.Split("=")
    if ($items[0] -eq "") { $SamAccountName = $items[1] }
  
  
  
  # Enable the account
  Enable-ADAccount -Identity $samAccountName
  
  # Remove Group Memberships
  (GET-ADUSER –Identity $samAccountName –Properties MemberOf | Select-Object MemberOf).MemberOf | Remove-ADGroupMember -Members $samAccountName 
  
  # Update Attributes
      #Remove from main dynamic distribution list
      Set-ADUser -Identity $samAccountName -company X1
      #Clear GAL field "Mail Box Type"
      Set-ADUser -Identity $samAccountName -Clear "extensionAttribute1"
      #Remove from team dynamic distribution list
      Set-ADUser -Identity $samAccountName -Department x2
      #Modify Description field with disable date and ticket number
      Set-ADUser -Identity $samAccountName -Description "disabled $Date Ticket $Ticket"
  
  # Move Account 
      Get-ADUser -Identity $samAccountName | move-adobject -targetpath "ou=disabled,ou=users,ou=division,dc=department,dc=company,dc=lcl" 
  
  # Disable Account
      Disable-ADAccount -Identity $samAccountName
  
  }
  </code></pre>