4

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.

5 Answers 5

2

Have a look at this:

http://blogs.msdn.com/b/adpowershell/archive/2009/05/05/how-to-create-a-function-to-validate-the-existence-of-an-ad-object-test-xadobject.aspx

2
$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.

4
  • 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, 2011 at 17:55
  • Maybe do something with -erroraction? Suppress the error and test for it?
    – Zoredache
    Sep 14, 2011 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, 2011 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, 2011 at 18:40
0

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

(Just kidding. Kind of.)

0

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).

2
  • Would this work with Get-ADComputer?
    – A P
    Apr 6, 2019 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, 2019 at 8:36
0
$errored=$false
try { Add-ADGroupMember -Identity $SomeGroupName $SomeComputer  }
catch {
    $errored=$true
    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
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .