Creating local users with Powershell

I am trying to setup a script to create a local user on a computer.  I can get a user created with all of the criteria I want without any issue.  The problem I am having is having the script check to see if the user exists first.  I have no idea where to go on this as I am very much new to powershell.  So far I create the user using the following:

Function NewUser {

# Prompts for new user information

Param (
[string]$Username=(Read-Host "Enter a username"),
[string]$FullName=(Read-Host "Enter the users full name"),
[String]$Password=(Read-Host "Enter a password")

# Create username and password from what was written in the prompts

$computer = [ADSI]"WinNT://$env:ComputerName"
$user = $computer.Create("User", $Username)
$User.FullName = "$FullName"

net localgroup Users "$Username" /add

There is more to it than that but I need to modify this to check to see if the user name exists before I go any further with it.

Any ideas?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ady FootSharePoint ConsultantCommented:
What about:

If you are using qad cmdlets:

If (get-qaduser username)
    #account exists, do something

If not:

if ([adsi]::Exists("LDAP://CN=username,DC=domain,DC=com"))
    #account exists, do something


Ady FootSharePoint ConsultantCommented:
This may help also - it's about searching for computers but the logic is the same:


mattcassellAuthor Commented:
The qad cmdlets are just for AD I thought and since this is a non-domain machine it will not work.

I have this to check and list what the local user accounts are:

$computerName ="localhost"
if ($computerName -eq "") {$computerName = "$env:computername"}  
$computer = [ADSI]"WinNT://$computerName,computer"  
$computer.psbase.Children | Where-Object { $_.psbase.schemaclassname -eq 'user' } | Format-Table Name

It works but I do not know how to compare the output against the "$username" variable in my original piece.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Ady FootSharePoint ConsultantCommented:

$objComputer = [ADSI]("WinNT://atl-ws-001,computer")

$colUsers = ($objComputer.psbase.children |
    Where-Object {$_.psBase.schemaClassName -eq "User"} |
        Select-Object -expand Name)

$blnFound = $colUsers -contains "kenmyer"

if ($blnFound)
    {"The user account exists."}
    {"The user account does not exist."}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ady FootSharePoint ConsultantCommented:
The above was taken from the following website which you might find useful for the full explanation as to how it works:


mattcassellAuthor Commented:
This does exactly what I want. I have simply modified it so that instead of it telling me the account does not exist it just goes ahead and creates it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.