ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    PowerShell - Create New AD User Using Prompts and Variables

    IT Discussion
    powershell ad windows
    6
    26
    4.9k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JaredBuschJ
      JaredBusch @wrx7m
      last edited by

      @wrx7m said in PowerShell - Create New AD User Using Prompts and Variables:

      @JaredBusch said in PowerShell - Create New AD User Using Prompts and Variables:

      This is why you run things manually and not in scripts until you know WTF you are doing.

      Use the PowerShell ISE, that is what it is for.

      You also do not have the Country in quotes. It is a string value.

      I am running it manually and I am running it in ISE.

      Change your New-ADUser to a Write-Host and keep adding to the concatenated line until it breaks.

      coliverC 1 Reply Last reply Reply Quote 1
      • coliverC
        coliver @JaredBusch
        last edited by coliver

        @JaredBusch said in PowerShell - Create New AD User Using Prompts and Variables:

        @wrx7m said in PowerShell - Create New AD User Using Prompts and Variables:

        @JaredBusch said in PowerShell - Create New AD User Using Prompts and Variables:

        This is why you run things manually and not in scripts until you know WTF you are doing.

        Use the PowerShell ISE, that is what it is for.

        You also do not have the Country in quotes. It is a string value.

        I am running it manually and I am running it in ISE.

        Change your New-ADUser to a Write-Host and keep adding to the concatenated line until it breaks.

        Write-host is one of the best debugging tools for poweshell. Does ISE have the ability to report what variable is assigned to what at a given step?

        1 Reply Last reply Reply Quote 1
        • wrx7mW
          wrx7m
          last edited by wrx7m

          Thanks, everyone. Write-host was a great tool. I have found the following to work from start to finish.

          Edit: Added GivenName and Surname parameters

          Import-Module ActiveDirectory
          $GivenName = Read-Host -Prompt "Enter First Name"
          $Surname = Read-Host -Prompt "Enter Last Name"
          $SecurePW = Read-Host -Prompt "Enter a Password" -AsSecureString
          New-ADUser -GivenName $GivenName `
                     -Surname $Surname `
                     -Name "$GivenName $Surname" `
                     -AccountPassword $SecurePW  `
                     -SamAccountName $GivenName `
                     -DisplayName "$GivenName $Surname" `
                     -PasswordNeverExpires $True `
                     -UserPrincipalName "[email protected]" `
                     -Country "US" `
                     -EmailAddress "[email protected]" `
                     -Path "OU=email1.com,OU=User Accounts,DC=upn,DC=com" `
                     -Enabled $True
          Set-ADUser -Identity "$GivenName" -Add @{Proxyaddresses="SMTP:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]", "smtp:[email protected]"}
          Add-ADGroupMember -Identity Office365Users -Members $GivenName
          
          JaredBuschJ 1 Reply Last reply Reply Quote 0
          • JaredBuschJ
            JaredBusch @wrx7m
            last edited by

            @wrx7m Now you have to make a stupid proof because what if I just had to enter each time and asked me for information?

            wrx7mW 1 Reply Last reply Reply Quote 1
            • wrx7mW
              wrx7m @JaredBusch
              last edited by

              @JaredBusch said in PowerShell - Create New AD User Using Prompts and Variables:

              @wrx7m Now you have to make a stupid proof because what if I just had to enter each time and asked me for information?

              I wonder if it would allow no first and last name before erroring out. It shouldn't be able to create it, because those variables are used to create the UPN. No UPN, no workie. I do know that you have to specify a password that adheres to the domain policy.

              1 Reply Last reply Reply Quote 0
              • PhlipElderP
                PhlipElder @wrx7m
                last edited by

                @wrx7m said in PowerShell - Create New AD User Using Prompts and Variables:

                If I get rid of the attempt to combine the 2 existing variables into a 3rd, I get this error.

                New-ADUser : A positional parameter cannot be found that accepts argument '+'.
                At \\FP02\it\Scripts\AD\AD-InitialUserCreationVariables.ps1:5 char:1
                + New-ADUser -Name "$GivenName $Surname" `
                + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException
                    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.NewADUser
                

                Like this I think:

                New-ADUser -Name "$($GivenName) $($Surname)"`
                

                From: https://blogs.technet.microsoft.com/stefan_stranger/2013/09/25/powershell-sub-expressions/

                1 Reply Last reply Reply Quote 1
                • 1
                • 2
                • 2 / 2
                • First post
                  Last post