PowerShell Script to add newuser

139 views Asked by At

I made this Line to add new users but the AD doesnt enable my account

New-ADUser (Read-Host "Enter Username") -AccountPassword (Read-Host -AsSecureString "Enter Password") | Enable-ADAccount

Why is that?

2

There are 2 answers

0
Chris Martin On

Try this instead

New-ADUser (Read-Host "Enter UserName") -AccountPassword (Read-Host -AsSecureString "Enter Password") -Enabled:$true

I've never piped a new user account anywhere, but you can enable the account in the same line.

2
Matt On

If you look at TechNet for New-ADUser cmdlet you can see the following buried in the parameter description for -passthru:

By default (i.e. if -PassThru is not specified), this cmdlet does not generate any output.

Which you can also see in practice.

PS C:\Users\matt> new-aduser "matt"

PS C:\Users\matt> 

You are essentially passing $null to Enable-ADAccount which is why nothing appears to be happening. You need to use the -PassThru switch, as stated above, then it should work.

PS C:\Users\matt> new-aduser "matt" -PassThru


DistinguishedName : CN=matt,CN=Users,DC=DOMAIN,DC=NET
Enabled           : False
GivenName         : 
Name              : matt
ObjectClass       : user
ObjectGUID        : 914fccdc-11ce-442e-b247-e4fd24f90023
SamAccountName    : matt
SID               : S-1-5-21-961215277-3068250917-3774519051-11002
Surname           : 
UserPrincipalName :