Script to Check for existing user and if not, create new user RRS feed

  • Question

  • Hey,

    I am new to Powershell and trying to create an algorithm that creates a new user but only after checking to see if the user exists.

    I have looked around online at what others have done and have been playing around with my code, but I can't get the script to run correctly and create a new user.

    This is what I have so far...

    Any help would be appreciated
    #Checking if user exists
    Function Get-ADUser {
    $Name = "$FirstName $LastName"

    $User = Get-ADUser =LDAPFilter"(SAMAccountName=$Name)"
    If ($User -eq $Null) {
        return $false
    Else {
        return $true

    #Checking if user exists

    Function NewADUser{

    Import-Module ActiveDirectoty -ErrorAction SilentlyContinue

        New-ADUser `
        -Name "$FirstName $LastName" `
        -GiveName $FirstName `
        -Surname $LastName `
        -UserPrincipalName "$FirstName.$LastName" `
        -Path "OU=Office Administration_OU,OU=Departments,DC=EC_Bond,DC=com" `
        -AccountPAssword (Read-Host -AsSecureString "Enter Password") -Enabled $true

    #Adding logic to script
    $ch = checkuserExists $NewUserFirstName $NewUserSurname

    [string]$NewUserFirstName = Read-Host -Prompt 'Please enter FirstName'
    [string]$NewUserSurname = Read-Host -Prompt 'Please enter Surname'

    If ($ch) {
        Write-Host "The user you are trying to create already Exists"
        Else {
            [string]$UserCreation = Read-Host -Prompt 'Would you like to create the user [Y] or [N]?'
            If ($UserCreation -eq "Y") {
                NewADUser $NewUserFirstName $NewUserSurname
            Else {
                #Abort process
                Write-Host "Process Aborted"

    Sunday, November 3, 2019 9:42 AM

All replies

  • Your first big mistake is very common to most new to coding and that is trying to wrap functions in another function. This is not necessary. CmdLets are functions and will do everything you need and be easier to understand and maintain. 

    The other big issue to that, without some formal study of PowerShell, yyou will have a very hard time undersanding why things don't work.  I recommend getting a good book on PowerShell for first time coders.  This will get you going much faster and you will skip learning the wrong things that you will get from copying code you do not understand and that is likely poorly written.  In other words - don't practice other peoples mistakes.

    This is how to start with PowerShell to do this:

    $samAccountname = Read-Host -Prompt 'Please enter desired SamAccountName'
    if(Get-ADUser -LDAPFilter "SAMAccountName -eq '$samAccountname'"){
         Write-Host 'The user you are trying to create already Exists'
        $firstName = Read-Host -Prompt 'Please enter FirstName'
    $lastName = Read-Host -Prompt 'Please enter Surname' $secpass = Read-Host 'Enter Password -AsSecureString' $userprops = @{ SamAccountName = $samAccountname Name = "$lastName $firstName" GiveName = $firstName Surname = $lastName UserPrincipalName = '{0}.{1}@domain.com' -f $firstName, $lastName Path = 'OU=Office Administration_OU,OU=Departments,DC=EC_Bond,DC=com' AccountPassword = $secpass Enabled = $true } New-ADUser @userprops -PassThru }
    The code you posted has inconsistent naming and numerous syntax and logic errors.  My example shows you how to approach this but still needs to be have error management added.  You also have to learn enough about AD to know how to obtain a unique SamAccountName and "Name" value.

    Without a good understanding of AD and Windows you will have some issues trying to write code.  I recommend searching the Gallery for the many scripts that crate new users and manage all of these issues.  After a while you will begin to understand why copying and guessing has caused you issues.

    Here are some links that will help you to get started with PowerShell. Take some time to study them.

    The PowerShell Best Practices and Style Guide

    How to post code in Technet Forums

    Getting Started with Microsoft PowerShell  

    Please edit your original post and follow these instructions: How to post code in Technet Forums


    • Edited by jrv Sunday, November 3, 2019 10:14 AM
    • Proposed as answer by BOfH-666 Sunday, November 3, 2019 11:27 AM
    Sunday, November 3, 2019 10:09 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, November 4, 2019 8:36 AM