PowerShell Help Add User

Ryck

Limp Gawd
Joined
Mar 7, 2003
Messages
332
Okay I am trying to add a user through PowerShell. Call it a learning experience, as I know how to do it through ADAC.

This is the script I am trying to use, but I keep getting errors.

$secpass = Read-Host "Password" -AsSecureString
New-ADUser -Name "User Name" -SamAccountName Uname
-UserPrincipalName "Unamen@test.com" -AccountPassword $secpass
-Path "cn=Users,dc=Test,dc=com" -Enabled:$true

Any help would be greatly appreciated.
 

socK

2[H]4U
Joined
Jan 25, 2004
Messages
3,942
Don't guess or make us guess. Post the error, it's literal purpose is to help you figure out what's wrong.

Do you actually have permissions to create the user? PowerShell will try under whatever user you're logged in as unless you specify the Credential parameter.
 

Ryck

Limp Gawd
Joined
Mar 7, 2003
Messages
332
Sorry for the delay, one thing after another and they just don't stop. Real life has just been to much and I have not had time to fire up the hyperv

Okay this is the error I am receiving when I use this code.


Import-Csv names.csv |
foreach {
$name = "$($_.LastName) $($_.FirstName)"

New-ADUser -GivenName $($_.FirstName) -Surname $($_.LastName)
-Name $name -SamAccountName $($_.SamAccountName)
-UserPrincipalName "$($_.SamAccountName)@test.com"
-AccountPassword $secpass -Path "cn=Users,dc=test,dc=com"
-Enabled:$true



and this is the original error that I received when I used this code

$secpass = Read-Host "Password" -AsSecureString
New-ADUser -Name "User Name" -SamAccountName Uname
-UserPrincipalName "Unamen@test.com" -AccountPassword $secpass
-Path "cn=Users,dc=Test,dc=com" -Enabled:$true


And thanks as always.
 

Attachments

socK

2[H]4U
Joined
Jan 25, 2004
Messages
3,942
You have the cmdlet bleeding onto multiple lines. Use a backtick character to signify you're breaking to a new line, or better yet - use splatting. It's IMO the most readable by far if you're writing a script.

Give me a second and I'll come up with an example

edit:
For the second error, I'd guess your Path is wrong somehow. If you're just sending users to the default Users container, you don't need to bother anyway and can just leave it out.

Bash:
$names = Import-Csv "test.csv"
$names | ForEach-Object {

    $ssPassword = Read-Host "Password" -AsSecureString
    $adArgs     =
    @{
        Enabled           = $true
        Path              = "cn=Users,dc=test,dc=com"
        GivenName         = $_.FirstName
        Surname           = $_.LastName
        DisplayName       = "$($_.FirstName) $($_.LastName)"
        SamAccountName    = $_.SamAccountName
        UserPrincipalName = "$($_.SamAccountName)@test.com"
        AccountPassword   = $ssPassword
    }

    New-ADUser @adArgs
}
 
Last edited:

Col_Temp

Weaksauce
Joined
Jun 1, 2020
Messages
101
You have the cmdlet bleeding onto multiple lines. Use a backtick character to signify you're breaking to a new line, or better yet - use splatting. It's IMO the most readable by far if you're writing a script.

Give me a second and I'll come up with an example

edit:
For the second error, I'd guess your Path is wrong somehow. If you're just sending users to the default Users container, you don't need to bother anyway and can just leave it out.

Bash:
$names = Import-Csv "test.csv"
$names | ForEach-Object {

    $ssPassword = Read-Host "Password" -AsSecureString
    $adArgs     =
    @{
        Enabled           = $true
        Path              = "cn=Users,dc=test,dc=com"
        GivenName         = $_.FirstName
        Surname           = $_.LastName
        DisplayName       = "$($_.FirstName) $($_.LastName)"
        SamAccountName    = $_.SamAccountName
        UserPrincipalName = "$($_.SamAccountName)@test.com"
        AccountPassword   = $ssPassword
    }

    New-ADUser @adArgs
}
What I was going to say but you beat me to it. Whyne you get an error with a legal tag look for broken commands.
Elegant solution you posted.
 
Top