How to check if computer name is already exist in the domain Powershell RRS feed

  • Question

  • My Powershell script used in each pc in our network to rename pc but i want to check domain is the new name is already exist Any help please

    $Searcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher
    $searcher = [adsisearcher]"(&(objectCategory=computer)(objectClass=computer)(name=$global:NewComputerName))"
    $searchResult = $searcher.FindAll()
    if($searchResult.count -eq 1)
      $Result = $true
      $Result = $False

    So then i use this to check if the result is true or false

    elseif ($Result -eq $True)
        $msgBoxInput7 = [System.Windows.Forms.MessageBox]::Show('computer is exist', 'OK')
         switch  ($msgBoxInput7) {
        'OK' {

    Please help me this code doesn’t work

    • Edited by iDr_Tech Monday, September 14, 2020 9:30 AM
    Monday, September 14, 2020 9:29 AM

All replies