I want to test for the existence of a computer account in Active Directory with PowerShell using only tools from Microsoft.

Using Quest AD cmdlets, I can do this:

if (!(get-qadcomputer $name)){ Stuff }

That doesn't work with get-adobject or get-adcomputer as far as I can tell.

Is there something simple that I'm missing? I've seen a couple hacky-looking solutions that trap all exceptions that get thrown, but that seems like it could give some false positives under certain circumstances.


Have a look at this:


$c = Get-ADComputer <$ComputerName>
if($c -eq $null) { ItDoesntExist } else { ItLives }

This should do exactly what you need... you said it isn't working for you, why exactly?

Sorry, looks like this cmdlet actually throws an exception instead of simply returning $null, as documented here... and it also ignores the -erroraction parameter (scroll down to the comments on the linked page).

Suggested workaround:

$errorActionPreference = "SilentlyContinue"

Get-ADComputer <$ComputerName>

Or, better, see my other answer.

  • I get Get-ADComputer : Cannot find an object with identity: 'test' under: 'DC=my,DC=domain'. At line:1 char:20 + $c = Get-ADComputer <<<< "test" + CategoryInfo : ObjectNotFound: (test:ADComputer) [Get-ADComputer], ADIdentityNotFoundException + FullyQualifiedErrorId : Cannot find an object with identity: 'test' under: 'DC=my,DC=domain'.,Microsoft.ActiveDirectory.Ma nagement.Commands.GetADComputer when running it against a machine that doesn't exist.
    – MDMarra
    Sep 14 '11 at 17:55
  • Maybe do something with -erroraction? Suppress the error and test for it?
    – Zoredache
    Sep 14 '11 at 18:23
  • Ops. Looks like it's a known (and frustrating) issue: blogs.technet.com/b/askds/archive/2010/02/04/… (scroll down to the comments).
    – Massimo
    Sep 14 '11 at 18:28
  • Yeah, not returning $null like a good cmdlet is what's been killing me. I'm trying out your other answer now, though.
    – MDMarra
    Sep 14 '11 at 18:40

Try to delete it and read the return code for that?

(Just kidding. Kind of.)


To test an AD Object in general by DistinguishedName, you can use the following:

[bool](Get-ADObject -Filter {DistinguishedName-eq "CN=Users,DC=domain,DC=local"})

Or, when you want to search by another property like samaccountname:

[bool](Get-ADObject -Filter {sAMAccountname -eq "Administrator"})

Both will return only a boolean ($true or $false) and can be used in conditional logic (if/else, etc).

  • Would this work with Get-ADComputer? Apr 6 '19 at 16:36
  • 1
    This works with a lot of cmdlets which return an object or not. If it returns an object (or more) and you cast it to type boolean it will become a true, without an object returned it will become a false. Apr 8 '19 at 8:36
try { Add-ADGroupMember -Identity $SomeGroupName $SomeComputer  }
catch {
    Write-Output "$SomeComputer does not exist in AD,$_" | Tee-Object $errorLogFileName -Append
if ( $errored == $false) { 
    #either accept the computer is added to $SomeGroupName or delete it back out now that you know $someComputer exists

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.