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
LVL 1
mikeydkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 CastroEE Solution Guide - CEO Faru Bonon ITCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

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 CastroEE Solution Guide - CEO Faru Bonon ITCommented:
He just ignores our answers hahaha :P
0
mikeydkAuthor Commented:
Jose》 you did not read my question
0
Jose Gabriel Ortega CastroEE Solution Guide - CEO Faru Bonon ITCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.