none
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))"
    $searcher.PropertiesToLoad.AddRange(('name'))
    $searchResult = $searcher.FindAll()
    if($searchResult.count -eq 1)
     {
      $Result = $true
     }
      else
    {
      $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' {
                    $groupbox1.ResumeLayout()
                    $form1.ResumeLayout()
                    $form1.add_FormClosed($Form_Cleanup_FormClosed)
             }
         }
    }

    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