Test for existence of an AD object in PowerShell using only Microsoft-provided cmdlets

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


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