none
PowerShell Adding New User to AD by Coping Old User RRS feed

  • Question

  • I am trying to create a new user from an old user.  I have to following code that I am using but. I am stuck on how to add the code to take the old user and add the new user.  Below is the code.  I am trying to write this in PowerShell as much as possable.  Thanks for any help I can get.

    Import-Module ShowUI
    Import-Module ActiveDirectory

    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null

    $form=New-Object system.windows.forms.Form
        $form.Height=625
        $form.Width=300
        $form.StartPosition="CenterScreen"
        $form.Text="Create New users"
        $form.add_Load($Form_Load)
        $form.FormBorderStyle=3

    ## Form Objects
        [System.Windows.Forms.Application]::EnableVisualStyles()
                   $txtDomain = New-Object System.Windows.Forms.TextBox
                   $txtOU = New-Object System.Windows.Forms.TextBox
                   $txtCopyUser = New-Object System.Windows.Forms.TextBox
                   $txtFirstName = New-Object System.Windows.Forms.TextBox
                   $txtLastName = New-Object System.Windows.Forms.TextBox
                   $txtTitle = New-Object System.Windows.Forms.TextBox
                   $txtCompany = New-Object System.Windows.Forms.TextBox
                   $txtPhone = New-Object System.Windows.Forms.TextBox
                   $txtStreetAddress = New-Object System.Windows.Forms.TextBox
                   $txtCity = New-Object System.Windows.Forms.TextBox
                   $txtState = New-Object System.Windows.Forms.TextBox
                   $txtPostalCode = New-Object System.Windows.Forms.TextBox
                   $txtDisplayName = New-Object System.Windows.Forms.TextBox
                   $txtSamAccountName = New-Object System.Windows.Forms.TextBox
                   $txtUserPrincipalName = New-Object System.Windows.Forms.TextBox
                   $txtPassword = New-Object System.Windows.Forms.TextBox
                   $txtManager = New-Object System.Windows.Forms.TextBox
                   $txtDescription = New-Object System.Windows.Forms.TextBox
                   $txtSite = New-Object System.Windows.Forms.TextBox
                   $labelManager = New-Object System.Windows.Forms.Label
                   $labelPassword = New-Object System.Windows.Forms.Label
                   $labelUserPrincipalName = New-Object System.Windows.Forms.Label
                   $labelSamAccountName = New-Object System.Windows.Forms.Label
                   $labelDisplayName = New-Object System.Windows.Forms.Label
                   $labelPostalCode = New-Object System.Windows.Forms.Label
                   $labelState = New-Object System.Windows.Forms.Label
                   $labelCity = New-Object System.Windows.Forms.Label
                   $txtOffice = New-Object System.Windows.Forms.TextBox
                   $labelStreetAddress = New-Object System.Windows.Forms.Label
                   $labelPhone = New-Object System.Windows.Forms.Label
                   $labelDomain = New-Object System.Windows.Forms.Label
                   $labelOU = New-Object System.Windows.Forms.Label
                   $labelCopyUser= New-Object System.Windows.Forms.Label
                   $labelFirstName = New-Object System.Windows.Forms.Label
                   $labelLastName = New-Object System.Windows.Forms.Label
                   $labelOffice = New-Object System.Windows.Forms.Label
                   $labelTitle = New-Object System.Windows.Forms.Label
                   $labelDescription = New-Object System.Windows.Forms.Label
                   $labelDepartment = New-Object System.Windows.Forms.Label
                   $labelSite = New-Object System.Windows.Forms.Label
                   $labelCompany = New-Object System.Windows.Forms.Label
                   $cboDepartment = New-Object System.Windows.Forms.ComboBox
                   $btnSubmit = New-Object System.Windows.Forms.Button
                   $SB = New-Object System.Windows.Forms.StatusBar

    ## User Coding

        Function Set-SAMAccountName {
       
                $GivenName = $txtFirstName.Text
                $SurName = $txtLastName.Text
              
        return "{0}{1}" -f ($GivenName)[0],$SurName

        }

        Function Set-UPN {
       
            $GivenName = $txtFirstName.Text
            $SurName = $txtLastName.Text
            $Domain = $txtDomain.Text
           
         return "{0}{1}@{2}" -f ($GivenName)[0],$SurName,$Domain
       }

        Function Set-DisplayName {
       
            $GivenName = $txtFirstName.Text
            $SurName = $txtLastName.Text
           
          return "{0} {1}" -f $GivenName,$SurName

       }

        Function Set-Office {
           
                $SiteLocation = $txtSite.Text
                $State = $txtState.Text
               
          return "{0}{1}" -f $State,$SiteLocation
          }

        Function Set-Description {
           
                $JobTitle = $txtTitle.Text
               
          return $JobTitle}

        Function Copy-User {
            $User = Get-ADUser -Identity $txtCopyUser.Text
            }

        Function Add-User {
            New-ADUser -SamAccountName Set-SAMAccountName -Name Set-DisplayName -Instance Copy-User
           
            }

        Function OnApplicationExit {
            Remove-Module ActiveDirectory
            Remove-Module ShowUI
                $Script:ExitCode = 0}

        $txtName_TextChanged={
                $txtSamAccountName.Text = Set-sAMAccountName
                $txtDisplayName.Text = Set-DisplayName
                $txtUserPrincipalName.Text = Set-UPN
                $txtOffice.Text = Set-Office
                $txtDescription.Text = Set-Description
           }

        $txtSite_SelectedIndexChanged={
            $Site = $_.Site -match $txtSite.Text
            $txtSteetAddress.Text = $Site.StreetAddress
            $txtCity.Text = $Site.City
            $txtState.Text = $Site.State
            $txtPostalCode.Text = $Site.PostalCode
            }


    ## Array for Department

         $Departments=@("Your Departments")
       
        foreach($Department in $Departments){
            $cboDepartment.Items.add($Department)
            }

            $btnSubmit_Click={
                $CopyUser = $CopyUser.text
                $Name="$GivenName $Surname"
                $Domain = $txtDomain.Text
                $OU = $txtOU.Text
                $GivenName = $txtFirstName.Text
                $SurName = $txtLastName.Text
                $Office = $txtOffice.Text
                $Title = $txtTitle.Text
                $Manager = $txtManager.Text
                $Description = $txtDescription.Text
                $Department = $cboDepartment.Text
                $Company = $txtCompany
                $Phone = $txtPhone.Text
                $Site = $txtSite.Text
                $Street = $txtStreetAddress.Text
                $City = $txtCity.Text
                $State = $txtState.Text
                $Zip = $txtPostalCode.Text
                $DN = $txtDisplayName.Text
                $SAN = $txtSamAccountName.Text
                $UPN = $txtUserPrincipalName.Text
                $ChangePasswordAtLogon = $True
                $sAMAccountName = Set-sAMAccountName
                $userPrincipalName = Set-UPN
                $DisplayName = Set-DisplayName
                $AccountPassword = $txtPassword.text | ConvertTo-SecureString -AsPlainText -Force

                    $User = @{
                        Copyuser = $CopyUser.text
                        Name = $Name
                  GivenName = $GivenName
                  Surname = $Surname
                  Path = $OU
                  samAccountName = $samAccountName
                  userPrincipalName = $userPrincipalName
                  DisplayName = $DisplayName
                        Manager = $Manager
                  AccountPassword = $AccountPassword
                  ChangePasswordAtLogon = $ChangePasswordAtLogon
                  Enabled = $Enabled
                  OfficePhone = $Phone
                  Description = $Description
                  Title = $Title
                  Department = $Department
                  Company = $Company
                  Office = $Office
                  StreetAddress = $StreetAddress
                  City = $City
                  State = $State
                  PostalCode = $Zip
                     }
                           $SB.Text = "Coping User $CopyUser"
                     $SB.Text = "Creating new user $sAMAccountName"
                           $ADError = $Null
                       New-ADUser @User -ErrorVariable ADError
                                if ($ADError){$SB.Text = "[$sAMAccountName] $ADError"}
                                else{$SB.Text = "$sAMAccountName created successfully."}

            }


    ## Form Control
             
                $form.Controls.Add($txtCopyUser)
                $form.Controls.Add($txtSite)
                $form.Controls.Add($txtFirstName)
                $form.Controls.Add($txtLastName)
                $form.Controls.Add($txtTitle)
                $form.Controls.Add($txtManager)
                $form.Controls.Add($cboDepartment)
                $form.Controls.Add($txtPhone)
                $form.Controls.Add($txtStreetAddress)
                $form.Controls.Add($txtCity)
                $form.Controls.Add($txtState)
                $form.Controls.Add($txtPostalCode)
                $form.Controls.Add($btnSubmit)
                $form.Controls.Add($txtOU)
                $form.Controls.Add($txtDomain)
                $form.Controls.Add($labelDomain)
                $form.Controls.Add($labelOU)
                $form.Controls.Add($labelCopyUser)
                $form.Controls.Add($labelFirstName)
                $form.Controls.Add($labelLastName)
                $form.Controls.Add($labelOffice)
                $form.Controls.Add($labelTitle)
                $form.Controls.Add($labelDescription)
                $form.Controls.Add($labelSite)
                $form.Controls.Add($labelDepartment)
                $form.Controls.Add($txtOffice)
                $form.Controls.Add($txtCompany)
                $form.Controls.Add($labelCompany)
                $form.Controls.Add($labelPhone)
                $form.Controls.Add($txtDisplayName)
                $form.Controls.Add($txtSamAccountName)
                $form.Controls.Add($txtUserPrincipalName)
                $form.Controls.Add($labelStreetAddress)
                $form.Controls.Add($txtDescription)
                $form.Controls.Add($labelCity)
                $form.Controls.Add($labelState)
                $form.Controls.Add($labelPostalCode)
                $form.Controls.Add($labelDisplayName)
                $form.Controls.Add($labelSamAccountName)
                $form.Controls.Add($labelUserPrincipalName)
                $form.Controls.Add($txtPassword)
                $form.Controls.Add($labelPassword)
                $form.Controls.Add($labelManager)
                $form.Controls.Add($SB)

    ## txtCopyUser

        $txtCopyUser.Location = '130,24'
        $txtCopyUser.Size = '150,30'
        $txtCopyUser.TabStop = '1'
       
    ## labelCopyUser
       
        $labelCopyUser.Location= '30,23'
        $labelCopyUser.Size= '100,20'
        $labelCopyUser.Font="Verdana,11"
        $labelCopyUser.Text="CopyUser:"

    ## txtDomain

        $txtDomain.Location = '130,49'
        $txtDomain.Size = '150,30'
        $txtDomain.text = "You Domain"
        $txtDomain.ReadOnly = 'True'
       
    ## labelDomain

        $labelDomain.Location = '30,48'
        $labelDomain.Size = '100,20'
        $labelDomain.Font="Verdana,11"
        $labelDomain.Text="Domain:"

    ## txtOU

        $txtOU.Location = '130,74'
        $txtOU.Size = '150,30'
        $txtOU.Text = "CN=Users,DC=Your Domain,DC=COM"
        $txtOU.ReadOnly = 'True'
       
    ## labelOU

        $labelOU.Location = '30,73'
        $labelOU.Size = '100,20'
        $labelOU.Font="Verdana,11"
        $labelOU.Text="OU:"

    ## txtSite

        $txtSite.Location = '130,99'
        $txtSite.Size = '150,30'
        $txtSite.add_TextChanged($txtName_TextChanged)
        $txtSite.TabStop = '2'
       
    ## labelSite
       
        $labelSite.Location= '30,98'
        $labelSite.Size= '100,20'
        $labelSite.Font="Verdana,11"
        $labelSite.Text="Site:"
       
    ## txtFirstName

        $txtFirstName.Location = '130,123'
        $txtFirstName.Size = '150,30'
        $txtFirstName.add_TextChanged($txtName_TextChanged)


    ## labelFirstName
       
        $labelFirstName.Location= '30,122'
        $labelFirstName.Size= '100,20'
        $labelFirstName.Font="Verdana,11"
        $labelFirstName.Text="First Name:"

     ## txtLastName

        $txtLastName.Location = '130,148'
        $txtLastName.Size = '150,30'
        $txtLastName.add_TextChanged($txtName_TextChanged)


     ## labelLastName
       
        $labelLastName.Location= '30,147'
        $labelLastName.Size= '100,20'
        $labelLastName.Font="Verdana,11"
        $labelLastName.Text="Last Name:"

    ## txtOffice

        $txtOffice.Location = '130,173'
        $txtOffice.Size = '150,30'
        $txtOffice.ReadOnly = 'True'
       
    ## labelOffice
       
        $labelOffice.Location= '30,172'
        $labelOffice.Size= '100,20'
        $labelOffice.Font="Verdana,11"
        $labelOffice.Text="Office:"

    ## txtTitle

        $txtTitle.Location = '130,199'
        $txtTitle.Size = '150,30'
        $txtTitle.add_TextChanged($txtName_TextChanged)

    ## labelTitle
       
        $labelTitle.Location= '30,198'
        $labelTitle.Size= '100,20'
        $labelTitle.Font="Verdana,11"
        $labelTitle.Text="Title:"

    ## txtManager

        $txtManager.Location = '130,223'
        $txtManager.Size = '150,30'

    ## labelManager
       
        $labelManager.Location= '30,222'
        $labelManager.Size= '100,20'
        $labelManager.Font="Verdana,11"
        $labelManager.Text="Manager:"

    ## txtDescription

        $txtDescription.Location = '130,248'
        $txtDescription.Size = '150,30'
        $txtDescription.ReadOnly = 'True'

    ## labelDescription
       
        $labelDescription.Location= '30,247'
        $labelDescription.Size= '100,20'
        $labelDescription.Font="Verdana,11"
        $labelDescription.Text="Description:"

    ## cboDepartment

        $cboDepartment.Location = '130,273'
        $cboDepartment.Size = '150,30'

    ## labelDepartment
       
        $labelDepartment.Location= '30,272'
        $labelDepartment.Size= '100,20'
        $labelDepartment.Font="Verdana,11"
        $labelDepartment.Text="Dept:"

    ## txtCompany

        $txtCompany.Location = '130,299'
        $txtCompany.Size = '150,30'
        $txtCompany.Text = "Your Company"
        $txtCompany.ReadOnly = 'True'

    ## labelCompany
       
        $labelCompany.Location= '30,298'
        $labelCompany.Size= '100,20'
        $labelCompany.Font="Verdana,11"
        $labelCompany.Text="Company:"

    ## txtPhone

        $txtPhone.Location = '130,323'
        $txtPhone.Size = '150,30'

    ## labelPhone
       
        $labelPhone.Location= '30,322'
        $labelPhone.Size= '100,20'
        $labelPhone.Font="Verdana,11"
        $labelPhone.Text="Phone:"

    ## txtStreetAddress

        $txtStreetAddress.Location = '130,348'
        $txtStreetAddress.Size = '150,30'

    ## labelStreetAddress
       
        $labelStreetAddress.Location= '30,347'
        $labelStreetAddress.Size= '100,20'
        $labelStreetAddress.Font="Verdana,11"
        $labelStreetAddress.Text="Street:"

    ## txtCity

        $txtCity.Location = '130,373'
        $txtCity.Size = '150,30'

    ## labelCity
       
        $labelCity.Location= '30,372'
        $labelCity.Size= '100,20'
        $labelCity.Font="Verdana,11"
        $labelCity.Text="City:"

    ## txtState

        $txtState.Location = '130,398'
        $txtState.Size = '150,30'
        $txtState.add_TextChanged($txtName_TextChanged)

    ## labelState
       
        $labelState.Location= '30,397'
        $labelState.Size= '100,20'
        $labelState.Font="Verdana,11"
        $labelState.Text="State:"

    ## txtPostalCode

        $txtPostalCode.Location = '130,423'
        $txtPostalCode.Size = '150,30'

    ## labelPostalCode
       
        $labelPostalCode.Location= '30,422'
        $labelPostalCode.Size= '100,20'
        $labelPostalCode.Font="Verdana,11"
        $labelPostalCode.Text="Zip Code:"

    ## txtDisplayName

        $txtDisplayName.Location = '130,448'
        $txtDisplayName.Size = '150,30'
        $txtDisplayname.ReadOnly = 'True'

    ## labelDisplayName
       
        $labelDisplayName.Location= '30,447'
        $labelDisplayName.Size= '100,20'
        $labelDisplayName.Font="Verdana,11"
        $labelDisplayName.Text="DN:"

    ## txtSamAccountName

        $txtSamAccountName.Location = '130,473'
        $txtSamAccountName.Size = '150,30'
        $txtSamAccountName.ReadOnly = 'Ture'

    ## labelSamAccountName
       
        $labelSamAccountName.Location= '30,472'
        $labelSamAccountName.Size= '100,20'
        $labelSamAccountName.Font="Verdana,11"
        $labelSamAccountName.Text="SAN:"

    ## txtUserPrincipalName

        $txtUserPrincipalName.Location = '130,498'
        $txtUserPrincipalName.Size = '150,30'
        $txtUserPrincipalName.ReadOnly = 'True'

    ## labelUserPrincipalName
       
        $labelUserPrincipalName.Location= '30,497'
        $labelUserPrincipalName.Size= '100,20'
        $labelUserPrincipalName.Font="Verdana,11"
        $labelUserPrincipalName.Text="UPN:"

    ## txtPassword

        $txtPassword.Location = '130,523'
        $txtPassword.Size = '150,30'
        $txtPassword.Text = "Your Passwork"
        $txtPassword.UseSystemPasswordChar = $True
        $txtPassword.ReadOnly = 'True'

    ## labelPassword
       
        $labelPassword.Location= '30,522'
        $labelPassword.Size= '100,20'
        $labelPassword.Font="Verdana,11"
        $labelPassword.Text="Password:"

    ## btnSubmit

      $btnSubmit.Location = '205, 548'
      $btnSubmit.Size = '75, 25'
      $btnSubmit.Text = "Submit"
      $btnSubmit.UseVisualStyleBackColor = $True
      $btnSubmit.add_Click($btnSubmit_Click)


    $form.ShowDialog()

    Monday, December 30, 2013 5:21 PM

Answers

  • I am not asking you to rewrite my code.  It does everything that I want but this one thing.  I let you see the code to see what I was doing and thought it would be easer to help if you saw the code.  The part of the code I need help with is the following. 

    Bill,  I will ignore the rude underton of your above reply.  I understand that you probable get alot of questions that are asking to write there scripts but,  that was not my attention.  I beleave you do not need to rewrite the code to help with this one point.  I know what the code is doing.

    I too read this as a request to fix your code. If you just want to know how to copy a use4r then Chen has the method for QAD. AD CmdLets can be used in a similar way.

    Get the user template you want to copy and transfer all properties you need to have copied.

    Get-ADUser <userid> | New-AdUser -SamAccountName <samname> -DisplayName <name> -Name <unique name> -UpN userid@domain.com

    This will attempt to copy all matching properties that are allowed from the pipeline and take the remainder from the supplied parameters.

    That is all you need to do although0 you will have to determine which parameters are going to be changed.  samname, name, upn are all required to be different.

    Try this at a command prompt until you get your requirements figured out the transfer it to your form.

    I recommend using -WhatIf so you can repeat the tests without actually creating any accounts.  This will tell you which parameters are moissng or incorrect without actually creating the account when you are successful.


    ¯\_(ツ)_/¯

    • Marked as answer by rkassing Monday, December 30, 2013 7:21 PM
    Monday, December 30, 2013 7:17 PM

All replies

  • I am trying to create a new user from an old user.  I have to following code that I am using but. I am stuck on how to add the code to take the old user and add the new user.  Below is the code.  I am trying to write this in PowerShell as much as possable.  Thanks for any help I can get.

    Import-Module ShowUI
    Import-Module ActiveDirectory

    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null

    $form=New-Object system.windows.forms.Form
        $form.Height=625
        $form.Width=300
        $form.StartPosition="CenterScreen"
        $form.Text="Create New users"
        $form.add_Load($Form_Load)
        $form.FormBorderStyle=3

    ## Form Objects
        [System.Windows.Forms.Application]::EnableVisualStyles()
                   $txtDomain = New-Object System.Windows.Forms.TextBox
                   $txtOU = New-Object System.Windows.Forms.TextBox
                   $txtCopyUser = New-Object System.Windows.Forms.TextBox
                   $txtFirstName = New-Object System.Windows.Forms.TextBox
                   $txtLastName = New-Object System.Windows.Forms.TextBox
                   $txtTitle = New-Object System.Windows.Forms.TextBox
                   $txtCompany = New-Object System.Windows.Forms.TextBox
                   $txtPhone = New-Object System.Windows.Forms.TextBox
                   $txtStreetAddress = New-Object System.Windows.Forms.TextBox
                   $txtCity = New-Object System.Windows.Forms.TextBox
                   $txtState = New-Object System.Windows.Forms.TextBox
                   $txtPostalCode = New-Object System.Windows.Forms.TextBox
                   $txtDisplayName = New-Object System.Windows.Forms.TextBox
                   $txtSamAccountName = New-Object System.Windows.Forms.TextBox
                   $txtUserPrincipalName = New-Object System.Windows.Forms.TextBox
                   $txtPassword = New-Object System.Windows.Forms.TextBox
                   $txtManager = New-Object System.Windows.Forms.TextBox
                   $txtDescription = New-Object System.Windows.Forms.TextBox
                   $txtSite = New-Object System.Windows.Forms.TextBox
                   $labelManager = New-Object System.Windows.Forms.Label
                   $labelPassword = New-Object System.Windows.Forms.Label
                   $labelUserPrincipalName = New-Object System.Windows.Forms.Label
                   $labelSamAccountName = New-Object System.Windows.Forms.Label
                   $labelDisplayName = New-Object System.Windows.Forms.Label
                   $labelPostalCode = New-Object System.Windows.Forms.Label
                   $labelState = New-Object System.Windows.Forms.Label
                   $labelCity = New-Object System.Windows.Forms.Label
                   $txtOffice = New-Object System.Windows.Forms.TextBox
                   $labelStreetAddress = New-Object System.Windows.Forms.Label
                   $labelPhone = New-Object System.Windows.Forms.Label
                   $labelDomain = New-Object System.Windows.Forms.Label
                   $labelOU = New-Object System.Windows.Forms.Label
                   $labelCopyUser= New-Object System.Windows.Forms.Label
                   $labelFirstName = New-Object System.Windows.Forms.Label
                   $labelLastName = New-Object System.Windows.Forms.Label
                   $labelOffice = New-Object System.Windows.Forms.Label
                   $labelTitle = New-Object System.Windows.Forms.Label
                   $labelDescription = New-Object System.Windows.Forms.Label
                   $labelDepartment = New-Object System.Windows.Forms.Label
                   $labelSite = New-Object System.Windows.Forms.Label
                   $labelCompany = New-Object System.Windows.Forms.Label
                   $cboDepartment = New-Object System.Windows.Forms.ComboBox
                   $btnSubmit = New-Object System.Windows.Forms.Button
                   $SB = New-Object System.Windows.Forms.StatusBar

    ## User Coding

        Function Set-SAMAccountName {
       
                $GivenName = $txtFirstName.Text
                $SurName = $txtLastName.Text
              
        return "{0}{1}" -f ($GivenName)[0],$SurName

        }

        Function Set-UPN {
       
            $GivenName = $txtFirstName.Text
            $SurName = $txtLastName.Text
            $Domain = $txtDomain.Text
           
         return "{0}{1}@{2}" -f ($GivenName)[0],$SurName,$Domain
       }

        Function Set-DisplayName {
       
            $GivenName = $txtFirstName.Text
            $SurName = $txtLastName.Text
           
          return "{0} {1}" -f $GivenName,$SurName

       }

        Function Set-Office {
           
                $SiteLocation = $txtSite.Text
                $State = $txtState.Text
               
          return "{0}{1}" -f $State,$SiteLocation
          }

        Function Set-Description {
           
                $JobTitle = $txtTitle.Text
               
          return $JobTitle}

        Function Copy-User {
            $User = Get-ADUser -Identity $txtCopyUser.Text
            }

        Function Add-User {
            New-ADUser -SamAccountName Set-SAMAccountName -Name Set-DisplayName -Instance Copy-User
           
            }

        Function OnApplicationExit {
            Remove-Module ActiveDirectory
            Remove-Module ShowUI
                $Script:ExitCode = 0}

        $txtName_TextChanged={
                $txtSamAccountName.Text = Set-sAMAccountName
                $txtDisplayName.Text = Set-DisplayName
                $txtUserPrincipalName.Text = Set-UPN
                $txtOffice.Text = Set-Office
                $txtDescription.Text = Set-Description
           }

        $txtSite_SelectedIndexChanged={
            $Site = $_.Site -match $txtSite.Text
            $txtSteetAddress.Text = $Site.StreetAddress
            $txtCity.Text = $Site.City
            $txtState.Text = $Site.State
            $txtPostalCode.Text = $Site.PostalCode
            }


    ## Array for Department

         $Departments=@("Your Departments")
       
        foreach($Department in $Departments){
            $cboDepartment.Items.add($Department)
            }

            $btnSubmit_Click={
                $CopyUser = $CopyUser.text
                $Name="$GivenName $Surname"
                $Domain = $txtDomain.Text
                $OU = $txtOU.Text
                $GivenName = $txtFirstName.Text
                $SurName = $txtLastName.Text
                $Office = $txtOffice.Text
                $Title = $txtTitle.Text
                $Manager = $txtManager.Text
                $Description = $txtDescription.Text
                $Department = $cboDepartment.Text
                $Company = $txtCompany
                $Phone = $txtPhone.Text
                $Site = $txtSite.Text
                $Street = $txtStreetAddress.Text
                $City = $txtCity.Text
                $State = $txtState.Text
                $Zip = $txtPostalCode.Text
                $DN = $txtDisplayName.Text
                $SAN = $txtSamAccountName.Text
                $UPN = $txtUserPrincipalName.Text
                $ChangePasswordAtLogon = $True
                $sAMAccountName = Set-sAMAccountName
                $userPrincipalName = Set-UPN
                $DisplayName = Set-DisplayName
                $AccountPassword = $txtPassword.text | ConvertTo-SecureString -AsPlainText -Force

                    $User = @{
                        Copyuser = $CopyUser.text
                        Name = $Name
                  GivenName = $GivenName
                  Surname = $Surname
                  Path = $OU
                  samAccountName = $samAccountName
                  userPrincipalName = $userPrincipalName
                  DisplayName = $DisplayName
                        Manager = $Manager
                  AccountPassword = $AccountPassword
                  ChangePasswordAtLogon = $ChangePasswordAtLogon
                  Enabled = $Enabled
                  OfficePhone = $Phone
                  Description = $Description
                  Title = $Title
                  Department = $Department
                  Company = $Company
                  Office = $Office
                  StreetAddress = $StreetAddress
                  City = $City
                  State = $State
                  PostalCode = $Zip
                     }
                           $SB.Text = "Coping User $CopyUser"
                     $SB.Text = "Creating new user $sAMAccountName"
                           $ADError = $Null
                       New-ADUser @User -ErrorVariable ADError
                                if ($ADError){$SB.Text = "[$sAMAccountName] $ADError"}
                                else{$SB.Text = "$sAMAccountName created successfully."}

            }


    ## Form Control
             
                $form.Controls.Add($txtCopyUser)
                $form.Controls.Add($txtSite)
                $form.Controls.Add($txtFirstName)
                $form.Controls.Add($txtLastName)
                $form.Controls.Add($txtTitle)
                $form.Controls.Add($txtManager)
                $form.Controls.Add($cboDepartment)
                $form.Controls.Add($txtPhone)
                $form.Controls.Add($txtStreetAddress)
                $form.Controls.Add($txtCity)
                $form.Controls.Add($txtState)
                $form.Controls.Add($txtPostalCode)
                $form.Controls.Add($btnSubmit)
                $form.Controls.Add($txtOU)
                $form.Controls.Add($txtDomain)
                $form.Controls.Add($labelDomain)
                $form.Controls.Add($labelOU)
                $form.Controls.Add($labelCopyUser)
                $form.Controls.Add($labelFirstName)
                $form.Controls.Add($labelLastName)
                $form.Controls.Add($labelOffice)
                $form.Controls.Add($labelTitle)
                $form.Controls.Add($labelDescription)
                $form.Controls.Add($labelSite)
                $form.Controls.Add($labelDepartment)
                $form.Controls.Add($txtOffice)
                $form.Controls.Add($txtCompany)
                $form.Controls.Add($labelCompany)
                $form.Controls.Add($labelPhone)
                $form.Controls.Add($txtDisplayName)
                $form.Controls.Add($txtSamAccountName)
                $form.Controls.Add($txtUserPrincipalName)
                $form.Controls.Add($labelStreetAddress)
                $form.Controls.Add($txtDescription)
                $form.Controls.Add($labelCity)
                $form.Controls.Add($labelState)
                $form.Controls.Add($labelPostalCode)
                $form.Controls.Add($labelDisplayName)
                $form.Controls.Add($labelSamAccountName)
                $form.Controls.Add($labelUserPrincipalName)
                $form.Controls.Add($txtPassword)
                $form.Controls.Add($labelPassword)
                $form.Controls.Add($labelManager)
                $form.Controls.Add($SB)

    ## txtCopyUser

        $txtCopyUser.Location = '130,24'
        $txtCopyUser.Size = '150,30'
        $txtCopyUser.TabStop = '1'
       
    ## labelCopyUser
       
        $labelCopyUser.Location= '30,23'
        $labelCopyUser.Size= '100,20'
        $labelCopyUser.Font="Verdana,11"
        $labelCopyUser.Text="CopyUser:"

    ## txtDomain

        $txtDomain.Location = '130,49'
        $txtDomain.Size = '150,30'
        $txtDomain.text = "You Domain"
        $txtDomain.ReadOnly = 'True'
       
    ## labelDomain

        $labelDomain.Location = '30,48'
        $labelDomain.Size = '100,20'
        $labelDomain.Font="Verdana,11"
        $labelDomain.Text="Domain:"

    ## txtOU

        $txtOU.Location = '130,74'
        $txtOU.Size = '150,30'
        $txtOU.Text = "CN=Users,DC=Your Domain,DC=COM"
        $txtOU.ReadOnly = 'True'
       
    ## labelOU

        $labelOU.Location = '30,73'
        $labelOU.Size = '100,20'
        $labelOU.Font="Verdana,11"
        $labelOU.Text="OU:"

    ## txtSite

        $txtSite.Location = '130,99'
        $txtSite.Size = '150,30'
        $txtSite.add_TextChanged($txtName_TextChanged)
        $txtSite.TabStop = '2'
       
    ## labelSite
       
        $labelSite.Location= '30,98'
        $labelSite.Size= '100,20'
        $labelSite.Font="Verdana,11"
        $labelSite.Text="Site:"
       
    ## txtFirstName

        $txtFirstName.Location = '130,123'
        $txtFirstName.Size = '150,30'
        $txtFirstName.add_TextChanged($txtName_TextChanged)


    ## labelFirstName
       
        $labelFirstName.Location= '30,122'
        $labelFirstName.Size= '100,20'
        $labelFirstName.Font="Verdana,11"
        $labelFirstName.Text="First Name:"

     ## txtLastName

        $txtLastName.Location = '130,148'
        $txtLastName.Size = '150,30'
        $txtLastName.add_TextChanged($txtName_TextChanged)


     ## labelLastName
       
        $labelLastName.Location= '30,147'
        $labelLastName.Size= '100,20'
        $labelLastName.Font="Verdana,11"
        $labelLastName.Text="Last Name:"

    ## txtOffice

        $txtOffice.Location = '130,173'
        $txtOffice.Size = '150,30'
        $txtOffice.ReadOnly = 'True'
       
    ## labelOffice
       
        $labelOffice.Location= '30,172'
        $labelOffice.Size= '100,20'
        $labelOffice.Font="Verdana,11"
        $labelOffice.Text="Office:"

    ## txtTitle

        $txtTitle.Location = '130,199'
        $txtTitle.Size = '150,30'
        $txtTitle.add_TextChanged($txtName_TextChanged)

    ## labelTitle
       
        $labelTitle.Location= '30,198'
        $labelTitle.Size= '100,20'
        $labelTitle.Font="Verdana,11"
        $labelTitle.Text="Title:"

    ## txtManager

        $txtManager.Location = '130,223'
        $txtManager.Size = '150,30'

    ## labelManager
       
        $labelManager.Location= '30,222'
        $labelManager.Size= '100,20'
        $labelManager.Font="Verdana,11"
        $labelManager.Text="Manager:"

    ## txtDescription

        $txtDescription.Location = '130,248'
        $txtDescription.Size = '150,30'
        $txtDescription.ReadOnly = 'True'

    ## labelDescription
       
        $labelDescription.Location= '30,247'
        $labelDescription.Size= '100,20'
        $labelDescription.Font="Verdana,11"
        $labelDescription.Text="Description:"

    ## cboDepartment

        $cboDepartment.Location = '130,273'
        $cboDepartment.Size = '150,30'

    ## labelDepartment
       
        $labelDepartment.Location= '30,272'
        $labelDepartment.Size= '100,20'
        $labelDepartment.Font="Verdana,11"
        $labelDepartment.Text="Dept:"

    ## txtCompany

        $txtCompany.Location = '130,299'
        $txtCompany.Size = '150,30'
        $txtCompany.Text = "Your Company"
        $txtCompany.ReadOnly = 'True'

    ## labelCompany
       
        $labelCompany.Location= '30,298'
        $labelCompany.Size= '100,20'
        $labelCompany.Font="Verdana,11"
        $labelCompany.Text="Company:"

    ## txtPhone

        $txtPhone.Location = '130,323'
        $txtPhone.Size = '150,30'

    ## labelPhone
       
        $labelPhone.Location= '30,322'
        $labelPhone.Size= '100,20'
        $labelPhone.Font="Verdana,11"
        $labelPhone.Text="Phone:"

    ## txtStreetAddress

        $txtStreetAddress.Location = '130,348'
        $txtStreetAddress.Size = '150,30'

    ## labelStreetAddress
       
        $labelStreetAddress.Location= '30,347'
        $labelStreetAddress.Size= '100,20'
        $labelStreetAddress.Font="Verdana,11"
        $labelStreetAddress.Text="Street:"

    ## txtCity

        $txtCity.Location = '130,373'
        $txtCity.Size = '150,30'

    ## labelCity
       
        $labelCity.Location= '30,372'
        $labelCity.Size= '100,20'
        $labelCity.Font="Verdana,11"
        $labelCity.Text="City:"

    ## txtState

        $txtState.Location = '130,398'
        $txtState.Size = '150,30'
        $txtState.add_TextChanged($txtName_TextChanged)

    ## labelState
       
        $labelState.Location= '30,397'
        $labelState.Size= '100,20'
        $labelState.Font="Verdana,11"
        $labelState.Text="State:"

    ## txtPostalCode

        $txtPostalCode.Location = '130,423'
        $txtPostalCode.Size = '150,30'

    ## labelPostalCode
       
        $labelPostalCode.Location= '30,422'
        $labelPostalCode.Size= '100,20'
        $labelPostalCode.Font="Verdana,11"
        $labelPostalCode.Text="Zip Code:"

    ## txtDisplayName

        $txtDisplayName.Location = '130,448'
        $txtDisplayName.Size = '150,30'
        $txtDisplayname.ReadOnly = 'True'

    ## labelDisplayName
       
        $labelDisplayName.Location= '30,447'
        $labelDisplayName.Size= '100,20'
        $labelDisplayName.Font="Verdana,11"
        $labelDisplayName.Text="DN:"

    ## txtSamAccountName

        $txtSamAccountName.Location = '130,473'
        $txtSamAccountName.Size = '150,30'
        $txtSamAccountName.ReadOnly = 'Ture'

    ## labelSamAccountName
       
        $labelSamAccountName.Location= '30,472'
        $labelSamAccountName.Size= '100,20'
        $labelSamAccountName.Font="Verdana,11"
        $labelSamAccountName.Text="SAN:"

    ## txtUserPrincipalName

        $txtUserPrincipalName.Location = '130,498'
        $txtUserPrincipalName.Size = '150,30'
        $txtUserPrincipalName.ReadOnly = 'True'

    ## labelUserPrincipalName
       
        $labelUserPrincipalName.Location= '30,497'
        $labelUserPrincipalName.Size= '100,20'
        $labelUserPrincipalName.Font="Verdana,11"
        $labelUserPrincipalName.Text="UPN:"

    ## txtPassword

        $txtPassword.Location = '130,523'
        $txtPassword.Size = '150,30'
        $txtPassword.Text = "Your Passwork"
        $txtPassword.UseSystemPasswordChar = $True
        $txtPassword.ReadOnly = 'True'

    ## labelPassword
       
        $labelPassword.Location= '30,522'
        $labelPassword.Size= '100,20'
        $labelPassword.Font="Verdana,11"
        $labelPassword.Text="Password:"

    ## btnSubmit

      $btnSubmit.Location = '205, 548'
      $btnSubmit.Size = '75, 25'
      $btnSubmit.Text = "Submit"
      $btnSubmit.UseVisualStyleBackColor = $True
      $btnSubmit.add_Click($btnSubmit_Click)


    $form.ShowDialog()

    Monday, December 30, 2013 4:43 PM
  • If you have quest powershell module. You can start from here

    Get-QADUser 'SomeName' -export | New-QADUser -ParentContainer mydomain.local/test -Name 'newname' -SamAccountName someid -DisplayName 'new name' -FirstName new -LastName name -UserPassword 'PWD' -import


    Regards Chen V [MCTS SharePoint 2010]

    Monday, December 30, 2013 5:28 PM
  • So your question is really the following:

    Dear forum participants, I have this 200+ line PowerShell script, but I don't know how it works. Can someone please customize, debug, and/or rewrite this code for me to my specifications, all for free?

    If you need for someone to customize code to your specifications, you will need to hire a consultant or developer.

    If you have a specific question, feel free to ask, but to ask others to customize code to spec is not the purpose of this forum.

    Bill

    Monday, December 30, 2013 5:52 PM
    Moderator
  • I am not asking you to rewrite my code.  It does everything that I want but this one thing.  I let you see the code to see what I was doing and thought it would be easer to help if you saw the code.  The part of the code I need help with is the following. 

      Function Copy-User {
            $User = Get-ADUser -Identity $txtCopyUser.Text
            }

        Function Add-User {
            New-ADUser -SamAccountName Set-SAMAccountName -Name Set-DisplayName -Instance Copy-User

    and how to call it to the following:

    $User = @{
                  Copyuser = $CopyUser.text
                  Name = $Name
                  GivenName = $GivenName
                  Surname = $Surname
                  Path = $OU
                  samAccountName = $samAccountName
                  userPrincipalName = $userPrincipalName
                  DisplayName = $DisplayName
                  Manager = $Manager
                  AccountPassword = $AccountPassword
                  ChangePasswordAtLogon = $ChangePasswordAtLogon
                  Enabled = $Enabled
                  OfficePhone = $Phone
                  Description = $Description
                  Title = $Title
                  Department = $Department
                  Company = $Company
                  Office = $Office
                  StreetAddress = $StreetAddress
                  City = $City
                  State = $State
                  PostalCode = $Zip                 }
                           $SB.Text = "Coping User $CopyUser"
                     $SB.Text = "Creating new user $sAMAccountName"
                           $ADError = $Null
                       New-ADUser @User -ErrorVariable ADError
                                if ($ADError){$SB.Text = "[$sAMAccountName] $ADError"}
                                else{$SB.Text = "$sAMAccountName created successfully."} 

    Bill,  I will ignore the rude underton of your above reply.  I understand that you probable get alot of questions that are asking to write there scripts but,  that was not my attention.  I beleave you do not need to rewrite the code to help with this one point.  I know what the code is doing.

    Monday, December 30, 2013 7:02 PM
  • I am not asking you to rewrite my code.  It does everything that I want but this one thing.  I let you see the code to see what I was doing and thought it would be easer to help if you saw the code.  The part of the code I need help with is the following. 

    Bill,  I will ignore the rude underton of your above reply.  I understand that you probable get alot of questions that are asking to write there scripts but,  that was not my attention.  I beleave you do not need to rewrite the code to help with this one point.  I know what the code is doing.

    I too read this as a request to fix your code. If you just want to know how to copy a use4r then Chen has the method for QAD. AD CmdLets can be used in a similar way.

    Get the user template you want to copy and transfer all properties you need to have copied.

    Get-ADUser <userid> | New-AdUser -SamAccountName <samname> -DisplayName <name> -Name <unique name> -UpN userid@domain.com

    This will attempt to copy all matching properties that are allowed from the pipeline and take the remainder from the supplied parameters.

    That is all you need to do although0 you will have to determine which parameters are going to be changed.  samname, name, upn are all required to be different.

    Try this at a command prompt until you get your requirements figured out the transfer it to your form.

    I recommend using -WhatIf so you can repeat the tests without actually creating any accounts.  This will tell you which parameters are moissng or incorrect without actually creating the account when you are successful.


    ¯\_(ツ)_/¯

    • Marked as answer by rkassing Monday, December 30, 2013 7:21 PM
    Monday, December 30, 2013 7:17 PM
  • Thank you for your reply.  I will work on it.

    Monday, December 30, 2013 7:21 PM
  • Just to be clear, this would be a function and I would call it in the code under

    $User = @{

    or

    $btnSubmit_Click={

    Monday, December 30, 2013 7:25 PM
  • Hi,

    My intent was not to be rude. It's always best to ask a specific question instead of posting a couple hundred lines of code (twice! I had to merge the two questions into one) and then ask everyone to read through all of it, figure it out, and then fix it for you.

    I always recommend posting a short example script that contains only enough code needed to reproduce a specific problem, and ask a specific question. Be sure also to post exact error messages. Remember, we can't read your mind, and we can't see your screen.

    Bill

    Monday, December 30, 2013 7:40 PM
    Moderator
  • Point taken.  I will work on this.

    Thanks,

    Monday, December 30, 2013 7:50 PM
  • This is about the least you can do for a copy with AD CmdLets.  It copies all matching properties and updates the mandatory properties that are unique to the account.  Yu can add as many more as needed.  They will overwrite the copied parameters.

    get-aduser testuser -Properties *|
         New-ADUser `
              -Path 'ou=testou,dc=testnet,dc=local' `
              -Name testuser11 `
              -surname user11 `
              -GivenName test `
              -SamAccountName testuser11 `
              -AccountPassword (ConvertTo-SecureString 'Some12Temp34Password' -AsPlainText -Force)
    

    The added unique parameters can be directly taken from the screen controls text properties.

    You will need to add error management and recovery to make this work.

    This code is actually tested and works as intended.  Substitute values from your system to see it work.


    ¯\_(ツ)_/¯

    Monday, December 30, 2013 7:52 PM