How to copy information from a user template to a new user

Welcome Forums General PowerShell Q&A How to copy information from a user template to a new user

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #119029

    Participant
    Points: -9
    Rank: Member

    Hello!

    So I am trying to automate creating new users by copying pre-made templates, I've managed to copy the memberof section of the template but I would need manager to get copied as well but I cannot get it to work. Does anyone know how to make this work. I will post the code I have so far. I am kind of new to powershell just for your knowledge

     

    import-module ActiveDirectory

    #Input Prompts
    $Firstname = Read-Host 'What is users first name?'
    $Lastname = Read-Host 'What is users last name?'
    $UserID = Read-Host 'What is users ID?'
    $password = Read-Host "Enter a Password:" -assecurestring
    $manager = Read-Host 'Manager'

    #Static Variables
    $credential = New-Object System.Management.Automation.PSCredential($password)
    $Displayname = "$lastname, $firstname"
    $OUPath = 'OU=Windows10,OU=SVKUsers,OU=Produktion,DC=svk,DC=local'

    #used to add parameters
    $Parameters = @{
    'SamAccountName' = $UserID
    'UserPrincipalName' = $UserID
    'Name' = $Firstname
    'GivenName' = $Firstname
    'Surname' = $Lastname
    'DisplayName' = $Displayname
    'AccountPassword' = $Password
    'ChangePasswordAtLogon' = $true
    'Enabled' = $true
    'Path' = $OUPath
    'Manager' = $manager

    }

    import-module activedirectory
    New-ADUser @Parameters -credential $domaincred
    Enable-ADAccount

    Start-Sleep -s 3

    #Detta ger alla grupper från den mall ni anger som source till eran ny skapade användare (destination user)

    write-output "This script adds the destination user to all the Groups which the source user is memberof."
    write-output " "
    write-output " "
    $SName = Read-Host "Please Enter the alias name of the source user "
    $DName = Read-Host "Please Enter the alias name of the Destination user "

    get-aduser -filter * -searchbase "ou=distributors,ou=people,dc=difc,dc=root01,dc=org" |
    foreach-object { Set-ADUser -identity $_distinguishedname -company $_.userPrincipalName }

    $K = Get-ADUser -Identity $SName -Properties memberOf
    foreach($group in $K.memberof)
    {
    Add-ADGroupMember -Identity $group -Member $DName
    write-output $group
    }

    Start-Sleep -s 3

  • #119035

    Participant
    Points: 814
    Helping Hand
    Rank: Major Contributor

    Well, there are many things I have to point out here. I believe you are giving this as a script.

    Avoid read hosts and use Parameters. Ideally key parameters are marked as mandatory, hence script will prompt if the value is not fed in. But since you need custom prompts, you can use Read-Host as the default value for parameters, so whoever want can call the script unattended by using parameters(hope you can understand this0

    No need to Import a module multiple times in a session, once imported the module will be there in the memory unless you close the session or remove explicitly.
    Enabling AD account can be done along with creation.

    Above are few that I modified and there are more. I would recommend you to start learning PowerShell.

    
    Param(
    
        $Firstname = $(Read-Host 'What is users first name?'),
    
        $Lastname = $(Read-Host 'What is users last name?'),
    
        $UserID = $(Read-Host 'What is users ID?'),
    
        [System.Security.SecureString]$Password = $( Read-Host "Enter a Password:" -AsSecureString),
    
        $Manager = $(Read-Host 'Manager'),
    
        $AliasName = $(Read-Host "Please Enter the alias name of the source user "),
    
        $DistinguishedName = $(Read-Host "Please Enter the alias name of the Destination user "),
    
        $OUPath = 'OU=Windows10,OU=SVKUsers,OU=Produktion,DC=svk,DC=local'
    )
    Import-Module ActiveDirectory
    #Static Variables
    $Displayname = "$Lastname, $Firstname"
    #used to add parameters
    $Parameters = @{
        SamAccountName        = $UserID
        UserPrincipalName     = $UserID
        Name                  = $Firstname
        GivenName             = $Firstname
        Surname               = $Lastname
        DisplayName           = $Displayname
        AccountPassword       = $Password
        ChangePasswordAtLogon = $true
        Enabled               = $true
        Path                  = $OUPath
        Manager               = $manager
        Password              = $Password
        Enabled               = $True
        Company               = $UserID
    }
    
    New-ADUser @Parameters
    
    #Detta ger alla grupper från den mall ni anger som source till eran ny skapade användare (destination user)
    write-output "This script adds the destination user to all the Groups which the source user is memberof. `n`n"
    
    Get-ADUser -Identity $AliasName -Properties memberOf | ForEach-Object -Process {
        Add-ADGroupMember -Identity $_-Member $DistinguishedName
        Write-Output "Adding $DistinguishedName to $_ group"
    }
    
    

    Btwn, I request you to format the code in the forum which makes other to easily understand your code, below link will help you.

You must be logged in to reply to this topic.