Error creating a Active Directory OU =

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 "$" `
        -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

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

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.

Welcome to the forum. :wave:t4:

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.

1 Like

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 …
  •   + 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.

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.

Thnks Floppy makes perfect sense.

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.

My comment was not about your error.

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