# Error creating a Active Directory OU =

**URL:** <https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771>\
**Category:** PowerShell Help\
**Created:** [February 28, 2023, 2:59am UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771 "2023-02-28T02:59:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bet0nBlack](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@Bet0nBlack](https://forums.powershell.org/u/Bet0nBlack)\
**Post date:** [February 28, 2023, 2:59am UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/1 "2023-02-28T02:59:34Z")

</div>

Hello group I am new to Power-Shell and am trying to learn at a fair speed. Trying to take some code and modify it. Any help would be great.

I have some basic codding See Errors  
-Path : The term ‘-Path’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling  
of the name, or if a path was included, verify that the path is correct and try again.

What am i missing here.

* * *

See Code below:

# Enabled the execution of all scripts - disables a sercurity policy

Set-ExecutionPolicy Unrestricted

# Import active directory module for running AD cmdlets

Import-Module activedirectory

# Password for users variables

$PASSWORD\_FOR\_USERS = “Welcome!1qaz”

# Takes the names from the names.txt and put them into $USER\_..

# Note: Your PS C: prompt has to be run from the directory when the text names is in.

$USER\_FIRST\_LAST\_LIST = Get-Content .\names.txt

# Takes password and converts it to plaintext

$password = ConvertTo-SecureString $PASSWORD\_FOR\_USERS -AsPlainText -Force

# creates a new org ion AD only if the Import-Module activedirectory cmd has been run

New-ADOrganizationalUnit -Name ‘Test Users’ -ProtectedFromAccidentalDeletion $false

# $n is the user in the names.txt list.

# The code will split the first and last name make them lowercase and store in the variables $Firstname etc.

foreach ($n in $USER\_FIRST\_LAST\_LIST) {  
$firstname = $n.Split(" “)[0].ToLower()  
$lastname = $n.Split(” ")[1].ToLower()  
$username = “$($firstname).$($lastname)”.ToLower()

# Gives an output of the names.

```
Write-Host "Creating user: $($username)" -BackgroundColor Black -ForegroundColor Cyan

New-ADUser -AccountPassword $password `
        -EmployeeID $username `
        -PasswordNeverExpires $false `
        -SamAccountName $username `
        -UserPrincipalName "$username@mybdomain.com" `
        -Name "$firstname $lastname" `
        -GivenName $firstname `
        -Surname $lastname `
        -DisplayName "$lastname, $firstname" `
        # -Path $OU `
        #-City $city `
        -Company $company `
        -State $state `
        -StreetAddress $streetaddress `
        -OfficePhone $telephone `
        -EmailAddress $email `
        -Title $jobtitle `
        -Department $department `
        -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True
        -Path "ou=Test Users,$(([ADSI]`"").distinguishedName)" `
        -Enabled $true
}

```

---

<div class="post-metadata">

**Author:** ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)\
**Post date:** [February 28, 2023, 3:19am UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/2 "2023-02-28T03:19:28Z")

</div>

You’re missing a backtick in -AccountPassword line after $True. So it thinks -Path is a continuation of the -AccountPassword command.

---

<div class="post-metadata">

**Author:** ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)\
**Post date:** [February 28, 2023, 3:21am UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/3 "2023-02-28T03:21:14Z")

</div>

I would also remove the 2 commented out arguments. When I copy your code, it thinks -Company is a cmdlet not an argument. Which is the same thing that’s happening to -Path with the missing back tick.

---

<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:** [February 28, 2023, 7:45am UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/4 "2023-02-28T07:45:57Z")

</div>

Everton,  
Welcome to the forum. 👋🏽

Using backticks as line continuation characters is considered very bad style and it is very error prone. Instead you should use splatting to make your code easier to read.

> **[about Splatting - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.3)**
>
> Describes how to use splatting to pass parameters to commands in PowerShell.

---

<div class="post-metadata">

**Author:** ![Bet0nBlack](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@Bet0nBlack](https://forums.powershell.org/u/Bet0nBlack)\
**Post date:** [February 28, 2023, 6:51pm UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/5 "2023-02-28T18:51:19Z")

</div>

Big Thanks Floppy. Was able to modify the text to work only issue is the error because the OU is already created.  
PS C:\Users\adm-efoster\Desktop\AD\_PS-master\> C:\Users\adm-efoster\Desktop\AD\_PS-master\Untitled1.ps1  
New-ADOrganizationalUnit : An attempt was made to add an object to the directory with a name that is already in use  
At C:\Users\adm-efoster\Desktop\AD\_PS-master\Untitled1.ps1:20 char:1

- New-ADOrganizationalUnit -Name ‘Test Users’ -ProtectedFromAccidentalD …
- 

```auto
  + CategoryInfo : NotSpecified: (OU=Test Users,DC=mybdomain,DC=com:String) [New-ADOrganizationalUnit], ADException
  + FullyQualifiedErrorId : ActiveDirectoryServer:8305,Microsoft.ActiveDirectory.Management.Commands.NewADOrganizationalUnit

```

Creating user: green.goblin.

Is there a way to add a if created ignore else create to the above script.

---

<div class="post-metadata">

**Author:** ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)\
**Post date:** [February 28, 2023, 8:39pm UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/6 "2023-02-28T20:39:10Z")

</div>

If you’re just creating a user in an existing OU, you dont need to use New-ADOrganizationalUnit. The New-ADUser will suffice.

If you just want to continue with the errors, you add $Erroractionpreference = ‘SilentlyContinue’ before running the commands.

---

<div class="post-metadata">

**Author:** ![Bet0nBlack](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@Bet0nBlack](https://forums.powershell.org/u/Bet0nBlack)\
**Post date:** [February 28, 2023, 11:00pm UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/7 "2023-02-28T23:00:04Z")

</div>

Thnks Floppy makes perfect sense.

---

<div class="post-metadata">

**Author:** ![Bet0nBlack](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@Bet0nBlack](https://forums.powershell.org/u/Bet0nBlack)\
**Post date:** [February 28, 2023, 11:02pm UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/8 "2023-02-28T23:02:13Z")

</div>

Olaf thanks for cluing me to the error. I will see if I can figure out the where to add the Splatting characters or do you know.

---

<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:** [February 28, 2023, 11:39pm UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/9 "2023-02-28T23:39:33Z")

</div>

> [@Bet0nBlack](#):
>
> Olaf thanks for cluing me to the error.

My comment was not about your error.

> [@Bet0nBlack](#):
>
> I will see if I can figure out the where to add the Splatting characters or do you know.

Please read the help. All of it. Including the examples.

---

<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:15pm UTC](https://forums.powershell.org/t/error-creating-a-active-directory-ou/21771/10 "2024-05-16T20:15:08Z")

</div>


