# Active Directory Help

**URL:** https://forums.powershell.org/t/active-directory-help/8622
**Category:** PowerShell Help
**Created:** [May 15, 2017, 2:10pm UTC](https://forums.powershell.org/t/active-directory-help/8622 "2017-05-15T14:10:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ocsscott](https://avatars.discourse-cdn.com/v4/letter/o/e9c0ed/32.png) [@ocsscott](https://forums.powershell.org/u/ocsscott)
#### Post date: [May 15, 2017, 2:10pm UTC](https://forums.powershell.org/t/active-directory-help/8622/1 "2017-05-15T14:10:25Z")

</div>

I am fairly new and need help on 2 active Directory tasks.

1. Checking if a pc name is in AD or not
2. adding a PC to AD

I saw Get-ADComputer but the module needed is not on all pc’s. these will be freshly imaged pc’s that need to be added to the domain. Is there another way or way to include any needed module in my script (it will be eventually compiled using Powershell studio)

For item 2 I want to add a pc if not in AD to the domain and a specific OU.

Any help would be great.

thanks

---

<div class="post-metadata">

### Author: ![julien-zweverink](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/julien-zweverink/32/367_2.png) [@julien-zweverink](https://forums.powershell.org/u/julien-zweverink)
#### Post date: [May 16, 2017, 2:55pm UTC](https://forums.powershell.org/t/active-directory-help/8622/2 "2017-05-16T14:55:30Z")

</div>

Here is a function to test if an Computer Account is in AD or not, and returns a Boolian(True,False)

```
Function Test-ADComputer {
#	.SYNOPSIS
# Helper Funtion that check's if an ADgroup (still) Exist in Active Directory.
#	
#	.DESCRIPTION
# Helper Funtion that check's if an ADgroup (still) Exist in Active Directory.
#	
#	.PARAMETER ComputerName
# The ActiveDirecory Computername / Identity
#	
#	.EXAMPLE
# PS C:\> Test-ADComputer -ComputerName $ComputerName
#		
# If (Test-ADComputer -ComputerName $ComputerName) {Do-SomeThing}
#	
#	.OUTPUTS
# System.Boolean
#	
#	.NOTES
# Made By Julien Zweverink
	[CmdletBinding(ConfirmImpact = 'None',
				   PositionalBinding = $true,
				   SupportsPaging = $false,
				   SupportsShouldProcess = $false)]
	[OutputType([bool])]
	param
	(
		[Parameter(Mandatory = $true,
				   ValueFromPipeline = $true,
				   ValueFromPipelineByPropertyName = $true,
				   ValueFromRemainingArguments = $false,
				   Position = 0)]
		[ValidateNotNullOrEmpty()]
		[Alias('CSName', 'PSComputerName', '__SERVER', 'HostName', 'Identity')]
		[string]$ComputerName
	)
	
	Begin {
		Write-Verbose -Message "[BEGIN] Starting: $($MyInvocation.Mycommand)"
	} #Begin
	
	Process {
		Try {
			Write-Verbose -Message "[Process] Test if: $ComputerName can be found in Active Directory"
			$null = Get-ADComputer -Identity $ComputerName -ErrorAction Stop
			Write-Verbose -Message "[Process] $ComputerName was found in Active Directory"
			return $true
		} #Try
		Catch {
			Write-Verbose "[Process] Computer: $ComputerName not found in Active Directory"
			Return $false
		} #Catch
	} #Process
	
	End {
		Write-Verbose -Message "[End] Ending: $($MyInvocation.Mycommand)"
	} #End
	
} #Function
```

To Use it:

```
Test-ADComputer -ComputerName Computer123 #-Verbose
#Or
If (Test-ADComputer -ComputerName Computer123) {'Do-SomeTing'}
```

---

<div class="post-metadata">

### Author: ![ocsscott](https://avatars.discourse-cdn.com/v4/letter/o/e9c0ed/32.png) [@ocsscott](https://forums.powershell.org/u/ocsscott)
#### Post date: [May 16, 2017, 5:03pm UTC](https://forums.powershell.org/t/active-directory-help/8622/3 "2017-05-16T17:03:33Z")

</div>

That wont work as it relies on Get-ADComputer. I asked for something that doesnt. these are new pc’s and that requires an install to work. It is also available as part of the Windows 7 and Windows 8 Remote Server Administration Toolkits (RSATs). It is not a Windows feature.

I need somethinbg out of the box

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [May 16, 2017, 5:18pm UTC](https://forums.powershell.org/t/active-directory-help/8622/4 "2017-05-16T17:18:52Z")

</div>

[Implicit Remoting](https://technet.microsoft.com/de-de/library/ff720181.aspx?f=255&MSPPError=-2147217396) might be helpful in this case …

---

<div class="post-metadata">

### Author: ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)
#### Post date: [May 17, 2017, 9:06am UTC](https://forums.powershell.org/t/active-directory-help/8622/5 "2017-05-17T09:06:42Z")

</div>

As ugly as this might be, it still works. You should do all that fancy stuff for the function as in above.

```
function get-ldapComputer ($computer) {

    $strFilter = "(&(objectCategory=computer)(Name=$computer))"
    
    $objDomain = New-Object System.DirectoryServices.DirectoryEntry
    
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter
    
    $objSearcher.PropertiesToLoad.Add('name') |out-null
    $result = $objSearcher.Findone()

    if ($result) {
        write-output "Found $($result.Properties.name)"
        }
    Else {
        write-output "DANG! There was no $computer"
        }
    }
```

---

<div class="post-metadata">

### Author: ![julien-zweverink](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/julien-zweverink/32/367_2.png) [@julien-zweverink](https://forums.powershell.org/u/julien-zweverink)
#### Post date: [May 19, 2017, 7:49am UTC](https://forums.powershell.org/t/active-directory-help/8622/6 "2017-05-19T07:49:08Z")

</div>

My bad,

This Module should do the trick, it uses ADSI and not the AD Module.

> **[GitHub - lazywinadmin/AdsiPS: PowerShell module to interact with Active...](https://github.com/lazywinadmin/AdsiPS)**
>
> PowerShell module to interact with Active Directory using ADSI and the System.DirectoryServices namespace (.NET Framework) - GitHub - lazywinadmin/AdsiPS: PowerShell module to interact with Active ...

---

<div class="post-metadata">

### Author: ![ocsscott](https://avatars.discourse-cdn.com/v4/letter/o/e9c0ed/32.png) [@ocsscott](https://forums.powershell.org/u/ocsscott)
#### Post date: [May 19, 2017, 8:24am UTC](https://forums.powershell.org/t/active-directory-help/8622/7 "2017-05-19T08:24:47Z")

</div>

Thanks guys the adsi and code samples worked perfect.

Solved

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/active-directory-help/8622/8 "2024-05-16T20:38:44Z")

</div>


