• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 87
  • Last Modified:

Create OU using Powershell

Powershell

How to create an OU based on distinguishedname (and all sub OUs)? (I have the following string "OU=MyLAB, OU=MyFirm, DC=FirmInc, DC=LOCAL")

Thx in advance

Mike
0
mikeydk
Asked:
mikeydk
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Hello ThereSystem AdministratorCommented:
If you want to create the OU in a location other than the domain root, you need to specify the distinguished name of the parent container.

NEW-ADOrganizationalUnit “YourNewOU” -Path "OU=MyFirm,DC=FirmInc,DC=Local"

This should be enough.
0
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
Just like hello said, I was testing it in my environment.
New-ADOrganizationalUnit -Path "OU=MyLAB, OU=MyFirm, DC=FirmInc, DC=LOCAL" -Name <nameoftheou>

Open in new window

0
 
oBdACommented:
Try this:
Function New-ADOrganizationalUnitRecurse([string]$DistinguishedName) {
	$OUPath, $Parent = $DistinguishedName -split ',(?=\s*DC=)', 2 | ForEach-Object {$_.Trim()}
	$OUList = @($OUPath -split '(?:\A|,)\s*OU=' | Where-Object {$_})
	[array]::Reverse($OUList)
	$OUList | ForEach-Object {
		$Name = $_
		Write-Host "Creating OU '$($Name)' in '$($Parent)' ..." -NoNewline
		Try {
			Get-ADOrganizationalUnit -Identity "OU=$($Name),$($Parent)" -ErrorAction Stop
			Write-Host " exists already."
		} Catch {
			New-ADOrganizationalUnit -Name $Name -Path $Parent -ErrorAction Stop -ProtectedFromAccidentalDeletion $true
			$Parent = "OU=$($Name),$($Parent)"
			Write-Host " OK."
		}
	}
}

Open in new window

0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
mikeydkAuthor Commented:
oBdA>

New-ADOrganizationalUnitRecurse -DistinguishedName "OU=TEST2,OU=MyFirm,DC=MyCompany,DC=LOCAL"

seems to create the "TEST2" in the root? (DC=MyCompany,DC=LOCAL)
0
 
Hello ThereSystem AdministratorCommented:
So my suggestion didn't work?
0
 
oBdACommented:
Sorry, setting the new parent was not in the correct place.
Corrected and enhanced; it now optionally returns the OU(s) that were created:
Function New-ADOrganizationalUnitRecurse([string]$DistinguishedName, [Switch]$PassThru) {
	$OUPath, $Parent = $DistinguishedName -split ',(?=\s*DC=)', 2 | ForEach-Object {$_.Trim()}
	$OUList = @($OUPath -split '(?:\A|,)\s*OU=' | Where-Object {$_})
	[array]::Reverse($OUList)
	Try {
		$OUList | ForEach-Object {
			$Name = $_
			Write-Host "Creating OU '$($Name)' in '$($Parent)' ..." -NoNewline
			Try {
				Get-ADOrganizationalUnit -Identity "OU=$($Name),$($Parent)" -ErrorAction Stop | Out-Null
				Write-Host " exists already."
			} Catch {
				$NewOU = New-ADOrganizationalUnit -Name $Name -Path $Parent -ErrorAction Stop -PassThru -ProtectedFromAccidentalDeletion $true
				Write-Host " OK."
				If ($PassThru) {
					$NewOU
				}
			}
			$Parent = "OU=$($Name),$($Parent)"
		}
	} Catch {
		Throw
	}
}

Open in new window


Hello There,
New-ADOrganizationalUnit will only create an OU if the parent exists. It does not automatically create any intermediate parent OUs that don't exist yet.
0
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
He just ignores our answers hahaha :P
0
 
mikeydkAuthor Commented:
Jose》 you did not read my question
0
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
Dude I did read it maybe I didn't understand what you wanted or you failed to explain yourself. Either way odba already answered so you didn't even mention to correct the answers but nvm if you want to think that it's OK I'm not arguing 😂

Maybe if you would tried or read the hello there or mine answer you would have get what you wanted.
0
 
mikeydkAuthor Commented:
based on distinguishedname ?
all sub OUs) ?

None of these questions was answered by you or Hello There...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now