Combining two scripts together

I am new to PowerShell and I have created 2 different scripts to add new users to Active Directory. Both of these scripts work on their own. So now I am trying to get “smart” and see if I can combine the 2 scripts together to run at the same time. But when I combine the scripts together the user accounts get created but the part to add the users to a “group” does not work.

Here is the script combined:

$NewUsers = Import-csv C:\New_User_Import\CC_New_Hire_Import_Template_Formulas.csv



Clear-Host

Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black

$SourceUser = Read-Host

"`n"



foreach ($User in $NewUsers)

{

$Username = $User.samaccountname

$Password = $User.password

$Firstname = $User.firstname

$Lastname = $User.lastname

$Displayname = $User.displayname

$description = $user.description

$office = $user.office

$userprincpalname = $user.userprincpalname

$OU = $user.oupath

$homeDirectory = $user.homeDirectory

$homeDrive = $user.homeDrive

$Email = $user.email

$Proxy = $User.proxyaddresses

$title = $user.title

$StreetAddress = $User.street

$City = $User.city

$State = $User.state

$PostalCode = $User.zip

#Check if the user account already exists in AD

if (Get-ADUser -F { SamAccountName -eq $Username })

{

#If user does exist, output a warning message

Write-Warning "A user account $Username already exist in Active Directory."

}

else

{

#If a user does not exist then create a new user account

#Account will be created in the OU listed in the $OU variable in the CSV file

New-ADUser

-SamAccountName $Username

-UserPrincipalName $userprincpalname

-Name "$Firstname $Lastname"

-GivenName $Firstname

-Surname $Lastname

-Description $description

-Office $office

-Enabled $True

-ChangePasswordAtLogon $True

-DisplayName $Displayname

-HomeDirectory $homeDirectory

-HomeDrive $homeDrive

-EmailAddress $Email

-Path $OU

-Title $title

-StreetAddress $StreetAddress

-City $City

-State $State

-PostalCode $PostalCode

-AccountPassword (convertto-securestring $Password -AsPlainText -Force) `

$SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object

{

$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name

}

foreach ($group in $SourceGroups)

{

Add-ADGroupMember -Identity $Group -Members $User

}

}

}

Here are the 2 scripts on their own…again, they both work this way:

#1

$NewUsers = Import-csv C:\New_User_Import\CC_New_Hire_Import_Template_Formulas.csv



foreach ($User in $NewUsers)

{

$Username = $User.samaccountname

$Password = $User.password

$Firstname = $User.firstname

$Lastname = $User.lastname

$Displayname = $User.displayname

$description = $user.description

$office = $user.office

$userprincpalname = $user.userprincpalname

$OU = $user.oupath

$homeDirectory = $user.homeDirectory

$homeDrive = $user.homeDrive

$Email = $user.email

$Proxy = $User.proxyaddresses

$title = $user.title

$StreetAddress = $User.street

$City = $User.city

$State = $User.state

$PostalCode = $User.zip

#Check if the user account already exists in AD

if (Get-ADUser -F { SamAccountName -eq $Username })

{

#If user does exist, output a warning message

Write-Warning "A user account $Username already exist in Active Directory."

}

else

{

#If a user does not exist then create a new user account

#Account will be created in the OU listed in the $OU variable in the CSV file

New-ADUser

-SamAccountName $Username

-UserPrincipalName $userprincpalname

-Name "$Firstname $Lastname"

-GivenName $Firstname

-Surname $Lastname

-Description $description

-Office $office

-Enabled $True

-ChangePasswordAtLogon $True

-DisplayName $Displayname

-HomeDirectory $homeDirectory

-HomeDrive $homeDrive

-EmailAddress $Email

-Path $OU

-Title $title

-StreetAddress $StreetAddress

-City $City

-State $State

-PostalCode $PostalCode

-AccountPassword (convertto-securestring $Password -AsPlainText -Force) 

Clear-Host

}

}

#2

Write-Host -object "Enter the username of the user whos groups you would like to copy" -foregroundcolor Green -backgroundcolor Black
		$SourceUser = Read-Host
		"`n"
		
		$SourceGroups = Get-ADUser $SourceUser -Property MemberOf | ForEach-Object
		{
			$_.MemberOf | Get-ADGroup | select Name -ExpandProperty Name | sort name
		}
		foreach ($group in $SourceGroups)
		{
			Add-ADGroupMember -Identity $Group -Members $User
		}

Any assistance or direction would be appreciated.

The PowerShell Best Practices and Style Guide