#Create NewUser Mailbox By Alberto Igbinedion
#Date created : 20/08/2015
#$Firstname = $_.firstname
#$Lastname = $_.lastname
#$Email =  $_.email

# Create NEw User and set Alias

# Create The Session
#$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://fa1rvwapxx398/PowerShell/ -Authentication Kerberos -Credential $UserCredential

# Import Your session
#Import-PSSession $Session -Verbose


function CreateNewUser
{
[cmdletbinding(SupportsShouldProcess=$True)]

Param($csvpath)

$csvpath = Import-Csv C:\Scripts\NewUsers.csv

 $csvpath | ForEach-Object {
$userPrincinpal = $_."samAccountName" + "@ons.gov.uk"
New-ADUser -Name $csvpath.Name -GivenName $csvpath.GivenName -Surname $csvpath.Surname -server FA1RPONSDC001 `
-Path "$_.ParentOU".trim `
-SamAccountName $_."samAccountName" `
-UserPrincipalName $userPrincinpal `
-AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) `
-ChangePasswordAtLogon $true `
-Enabled $true

$Email =  $_.Email
$Alias = $Email.Split("@")[0]

#Enable-MailUser -Identity $Alias -ExternalEmailAddress $Email

#Add-ADGroupMember "Domain Admins" $_."samAccountName";
}}

Import-Module ActiveDirectory
CreateNewUser "C:\Scripts\NewUsers.csv"


















<#
$Userlist = import-csv C:\Scripts\input\UserList.csv           
foreach ($User in $Userslist)            
{            
    $Displayname = $User.'Firstname' + " " + $User.'Lastname'            
    $UserFirstname = $User.'Firstname'            
    $UserLastname = $User.'Lastname'            
    $OU = $User.'OU'            
    $SAM = $User.'SAM'            
    $UPN = $User.'Firstname' + "." + $User.'Lastname' + "@" + $User.'Maildomain'            
    $Description = $User.'Description'            
    $Password = $User.'Password'            
    New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $false            
}



# $Alias = $Email.Split("@")[0]

    # Enable-MailUser $Alias
    # Set-Mailbox -Identity $SamAccountName -EmailAddressPolicyEnabled $True



     #Write-Host "$SamAccountName's MailBox Created" -ForegroundColor Green

<#

# Read Me Help Tips
<#
Remove Mailbox and leave the user AD Object:

    Disable-mailbox "Username" 

To take the 1st part (0) of string before the @ symbol of an email Testuser@ons.gov.uk Email
    $Alias=$Email.Split("@")[0]
To Replace any space with a .
    $Alias=$DisplayName.Replace('\s','.')[0]
    
    #>

    #New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true -server domain.loc