need a better way to create multiple OUs from csv

I have this notepad ++ file:

OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
OU=BBB,OU=222,OU=LOCATION,DC=corp,DC=local
OU=BBB,OU=333,OU=LOCATION,DC=corp,DC=local
.
.
.
<250 more unique lines)

and this script:

param([parameter(Mandatory=$true)] [String]$FileCSV)
$listOU=Import-CSV $FileCSV -Delimiter ";"
ForEach($OU in $listOU){
 
try{
#Get Name and Path from the source file
$OUName = $OU.Name
$OUPath = $OU.Path
 
#Display the name and path of the new OU
Write-Host -Foregroundcolor Yellow $OUName $OUPath
 
#Create OU
New-ADOrganizationalUnit -Name "$OUName" -Path "$OUPath"
 
#Display confirmation
Write-Host -ForegroundColor Green "OU $OUName created"
}catch{
 
Write-Host $error[0].Exception.Message
}
 
}

which is reading this csv:

name;path
Groups;OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Servers;OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Users;OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Workstations;OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local

…and realizing this is still gonna take a lot of manual work.

Is there a way to either massage the notepad ++ file to create the required (4) sub-OUs for all those Parent OUs in the first list?

thank you

New-ADOrganizationalUnit cmdlet supports pipeline input, so you can keep the headers of CSV file as same as input Parameters of the cmdlet then just pipe the Import-CSV output to New-ADOrganizationalUnit cmdlet.

If I understand, you have root or core OU that needs to be created first, then you can try to sort on Path.

name;path
Groups;OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Servers;OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Users;OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Workstations;OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
AAA;OU=111,OU=LOCATION,DC=corp,DC=local
Groups;OU=BBB,OU=111,OU=LOCATION,DC=corp,DC=local
Servers;OU=BBB,OU=111,OU=LOCATION,DC=corp,DC=local
Users;OU=BBB,OU=111,OU=LOCATION,DC=corp,DC=local
Workstations;OU=BBB,OU=111,OU=LOCATION,DC=corp,DC=local
BBB;OU=111,OU=LOCATION,DC=corp,DC=local

Sorting should have the shorter paths first so the root OU is created first and then the sub OUs:

name         path
----         ----
AAA          OU=111,OU=LOCATION,DC=corp,DC=local
BBB          OU=111,OU=LOCATION,DC=corp,DC=local
Groups       OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Servers      OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Users        OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Workstations OU=AAA,OU=111,OU=LOCATION,DC=corp,DC=local
Groups       OU=BBB,OU=111,OU=LOCATION,DC=corp,DC=local
Servers      OU=BBB,OU=111,OU=LOCATION,DC=corp,DC=local
Users        OU=BBB,OU=111,OU=LOCATION,DC=corp,DC=local
Workstations OU=BBB,OU=111,OU=LOCATION,DC=corp,DC=local

Another important piece is specifying a domain controller, otherwise you could create one OU on DC1 another on DC2 and then get errors because the root OU doesn’t exist:

$csv = Import-Csv -Path C:\Scripts\Temp.csv -Delimiter ';' |
       Sort-Object -Property Path

$csv | New-ADOrganizationalUnit -Server DC1

I have all my Root OUs created but I was looking for a way to create a better input file OR a better script for the input file (a list of various OU paths that were different for the first two levels, and then all the same after that). Thank you