new-aduser error

Hi

i wanted to create a script that will help me save time when i create a new user
the script is a one-liner and goes like that :
New-ADUser -Name (Read-Host “Name”) -GivenName (Read-Host “GivenName”) -Surname (Read-Host “SureName”) -SamAccountName (Read-Host “SamAccountName”) -Path (Read-Host “Path”) -Enabled $true -AccountExpirationDate $null -AccountPassword (Read-Host -AsSecureString “AccountPassword”)

when i run it and put all the parameters i get an error : “New-ADUser : The object name has bad syntax”

what am i doing wrong?

Thanks

Short answer is using Read-host in the way that you do.

I would either just type in the values, or turn your code into a simple function or create a csv file with the data and pass that to New-AdUser

Depends on what you typed in for the “Name” prompt, I think. (Path might be involved as well.)

By the way, this is the preferred way to prompt for input if a parameter hasn’t been passed in to your script. When you mark everything as Mandatory, the PowerShell engine takes care of that for you (including masking password input). It’s probably still using Read-Host or something similar to that under the hood, but you don’t need to know that. More importantly, this gives you the ability to run the script without all the prompts, if you want. (eg, .\NewADUser.ps1 ‘Bob Jones’ Bob Jones bjones ‘OU=Users,DC=Contoso,DC=com’ (‘p@ssw0rd’ | ConvertTo-SecureString -AsPlainText -Force) )

param (
    [Parameter(Mandatory = $true)]
    [string]
    $Name,

    [Parameter(Mandatory = $true)]
    [string]
    $GivenName,

    [Parameter(Mandatory = $true)]
    [string]
    $Surname,

    [Parameter(Mandatory = $true)]
    [string]
    $SamAccountName,

    [Parameter(Mandatory = $true)]
    [string]
    $Path,

    [Parameter(Mandatory = $true)]
    [System.Security.Securestring]
    $AccountPassword
)

New-ADUser @PSBoundParameters -Enabled $true -AccountExpirationDate $null

can you give me an example to a simple function?
why shouldn’t i use “read-host”

Thanks

$FileToImport = “C:\NewHires.csv”
import-csv $FileToImport |ForEach-Object { New-QADUser -name $.name -FirstName $.FirstName -LastName $.LastName -SamAccountName $.Samaccountname -Email $.email -userpassword $_.password -HomeDirectory $_.homedirectory -HomeDrive "U:" -Displayname $_.displayname -title $_.title -Department $_.department -office $_.office -streetaddress $_.streetaddress -city $_.city
-description ‘Class_06-02’ -stateorprovince $
.stateorprovince -postalcode $.postalcode -UserPrincipalName $.UserPrincipalName -ParentContainer your.domain/ou/users}

requires quest snap in

Read-Host is very inflexible. It just prompts you to enter something and that’s it.
However, parameterizing your one-liner, as Dave did, has many advantages. It makes your script look and act like a real PowerShell cmdlet, especially if you do the following. Amend Dave’s script like this:

Function New-CustomUser { param ( [Parameter(Mandatory = $true)] [string] $Name,
[Parameter(Mandatory = $true)]
[string]
$GivenName,

[Parameter(Mandatory = $true)]
[string]
$Surname,

[Parameter(Mandatory = $true)]
[string]
$SamAccountName,

[Parameter(Mandatory = $true)]
[string]
$Path,

[Parameter(Mandatory = $true)]
[System.Security.Securestring]
$AccountPassword

)

New-ADUser @PSBoundParameters -Enabled $true -AccountExpirationDate $null
}

In file explorer, go to “C:\Program Files\WindowsPowerShell\Modules” and create a folder called “New-CustomUser”.
Save the script in this folder as New-CustomUser.psm1. It’s very important that you save it with a .psm1 extension!
When it’s done, close PowerShell and open it again. In PowerShell ISE, start typing New-CustomUser and you will see that PowerShell recognizes your command just as any native cmdlet, like New-ADUser. You even get tab-completeion on the cmdlet and its parameters. The ISE will even show you the parameters thanks to autosense.
Actually, you have just written a script module that gets loaded automatically. How awesome it is!

The other problem with Read-Host is, that it’s not suitable for automation, since it cannot run unattended.

I don’t get what am I doing wrong with code formatting?
I use the code tags and the formatting just gets ugly.
On wordpress.com you can use and it works beautifully, even with color coding.

<div class="d4p-bbp-quote-title">Istvan Szarka wrote:</div>I don’t get what am I doing wrong with code formatting? I use the code tags and the formatting just gets ugly. On wordpress.com you can use [code language="powershell"][/code] and it works beautifully, even with color coding.

You are not alone to be annyoed by this. I think it is an issue with the BBPress plugin choosen.

Do you know which code formatting plugin Wordpress.com are using?

I don’t know. But Dave’s script is formatted alright, I wonder how does he do that.

For simple stuff, you can use PRE tags instead of CODE. If you need to post code with backticks, or embedded strings that look like HTML, I use a PowerShell function to escape the code before posting. See https://powershell.org/forums-tips/ for more information, under “Format Your Code”. The function I mentioned is in this blog post, and if you add the code to your ISE profile, you can just press F7 to escape the current ISE window’s code and copy it to your clipboard for pasting into a thread on the forums.

That said, you may see some changes in the WordPress / bbPress plugins being used here at some point in the near future, to make it more friendly for PowerShell. Don posted about this yesterday (https://powershell.org/2014/06/09/wish-list-better-code-formatting-in-the-forums-can-you-help/).

Edit: Nevermind, now the formatting in the blog post was screwed up too. Download it from TechNet instead.

Thanks you for all your helpful comments
I will try the function that you suggested

the @PSBoundParameters is a new concept to me
so i will follow up to learn more about

I see we’ve got very nice code formatting, thanks :slight_smile: