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.setpassword($Password)
$user.SetInfo()
$User.FullName = "$FullName"
$user.setinfo()

net localgroup Users "$Username" /add
}
NewUser


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?

Matt
mattcassellAsked:
Who is Participating?
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
}

Regards,

Ady
0
Ady FootSharePoint ConsultantCommented:
This may help also - it's about searching for computers but the logic is the same:
http://blogs.technet.com/heyscriptingguy/archive/2006/11/09/how-can-i-use-windows-powershell-to-get-a-list-of-all-my-computers.aspx

Regards,

Ady
0
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.
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

Ady FootSharePoint ConsultantCommented:
Try

$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."}
else
    {"The user account does not exist."}
0

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:
http://blogs.technet.com/heyscriptingguy/archive/2008/05/22/how-can-i-use-windows-powershell-to-determine-whether-a-local-user-account-exists.aspx

Regards,

Ady
0
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.
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.