Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: new-aduser error

$
0
0

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.


Viewing all articles
Browse latest Browse all 13067

Trending Articles