title: Searching if AD Computers Exists Using a Powershell Script
body: <p>I am trying to create a script that prompts the user to enter a computer name and then the script will return whether it exists on our domain or not, using the following.</p>
  
  <pre><code>$compname = Read-Host -Prompt "Please enter a computer name"
      $computers = Get-ADComputer -Filter * -SearchBase 'DC=LAN,DC=COMPANY,DC=COM' -SearchScope 2
  
      foreach($computer in $computers)
      {
      if($computer.Name -eq $compname)
      {
      Write-Output "$compname exists in your domain."
      }
      elseif($computer.Name -ne $compname)
      {
      Write-Output "$compname does not exist in your domain."
      }
  </code></pre>
  
  <p>This is working up until the <code>elseif</code> statement.
  If I search for a computer that I am 100% certain exists then the script returns as it should, but when I try a computer that I am 100% certain does not exist it returns nothing. </p>
  
  <p>Correct me if I am wrong but I believe this is because it is contained within the <code>foreach</code> statement. However I am struggling on where else to put the "does not contain" statement, or what to use.</p>
  
  <p>I am still very new to Powershell but I am trying to learn as much as possible and sometimes doing it is the key to learning. </p>
  
  <p>Note: I am aware that the searchbase is nondescript, this is for company security purposes but I can assure you that when I enter the correct OU, it works until the <code>elseif</code> as stated above.</p>
  
  <p>The next step would be to put this into a <code>do</code> <code>until</code> loop which will ask if the process wants to be repeated. </p>
  
  <p>Many thanks in advance</p>