locked
Powershell script delete computer RRS feed

  • Question

  • I want to delete computer which are in different domain. I loped it through domain using try and catch. However, I am looking for an output which for a computer if doesn't exist gives out the result. However, since i am looping a computer across all domains, it gives out result if a particular computer not part of a domain..i want a result if the computer is not part of each loop then gives out that result.

    any help?

    Sunday, January 12, 2020 11:52 AM

All replies

  • Question lacks details. But does this help?

    # Specify the Name of the computer.
    $Name = "MyComputer"
    
    # Retrieve all domains in the forest.
    $Domains = (Get-ADForest).Domains
    
    # Enumerate the domains.
    $Found = $False
    ForEach ($Domain In $Domains)
    {
        # Find the specified computer.
        $Computer = Get-ADComputer -Server $Domain -Filter {Name -eq "$Name"}
        If ($Computer)
        {
            # Computer found, do something.
            $Found = $True
        }
        Else
        {
            # Computer not found, do something.
        }
    }
    
    If ($Found) {"Computer found"}
    Else {"Computer not found in any domain"}
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Sunday, January 12, 2020 12:21 PM
  • Thanks for this. I guess this should be it..let me give a try.Thanks for this and sorry for not following the right process of posting questions
    Sunday, January 12, 2020 12:42 PM
  • 
    $Computers= get-content "C:\temp\computerslist.txt"
    $domains = (get-adforest).Domains
    
    $found=$false
    foreach ($computer in $Computers) 
    {
       Foreach ($domain in $Domains)
        {
            
            try
            {
    	Remove-ADComputer -Identity $computer -server $domain -confirm:$false –ErrorAction stop
        	$found=$true
            }
      
                  Catch 
            {
            #Write-host "$computer $domain user does not exist"
            
            }
    
        }
            if ($found){write-host $computer exist and deleted}
            Else { Write-host $computer not in any domain} 
    
    }
    

    This is what I am trying, but not able to get the desired result
    • Edited by CVRajesh Sunday, January 12, 2020 2:03 PM
    Sunday, January 12, 2020 2:02 PM
  • Again - you are not being helpful for yourself.  YOu need to read this link and try to understand how to ask a technical question. Just saying "it doesn't" work is of no help.

    How to ask questions in a technical forum

    As I suspected Rich's code was not the answer to your question because you question doesn't make any sense in English.  Read the link and ask a proper technical question that can be understood.

    Also read the following carefully:


    \_(ツ)_/

    Sunday, January 12, 2020 2:32 PM