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”
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) )
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,
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?
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.