Create Exchange mailbox from csv for some users

by Candee at 2013-04-10 08:16:22

I have a script that imports employee information, checks to see if the user exists in AD, and then creates the user accounts.
Some of the users will require mailboxes, but not all. I am having trouble with the if/then scenario.
I have a column in the csv “mailenabled” with true/false.
I want to check that column, if it’s false, create just the user; if it’s true, create the user and the mailbox.
Can someone point me in the right direction?

Here is my script:
Import-Csv $newusers |foreach {
$user = Get-QADUser -SamAccountName $.SamAccountName
if($user -ne $Null)
Write-host "$User name already exists "
.mailenabled eq false)
{#create the new users
New-qaduser -parentcontainer $OU -name $.name <br> -samaccountname $_.samaccountname
-displayname $
.displayname <br> -givenname $_.preferredfirstname
-sn $.lastnamepreferred <br> -userPassword $pass
-company $
.Company <br> -department $_.department -title $_.businesscardtitle -telephonenumber $_.telephone
-city $.city -postalcode $.zip -state $.state <br> -streetaddress $_.street -manager $_.manager
-oa @{ipphone=$
.ipphone;mobile=$.mobile;employeeid=$.employeeid;employeenumber=$.employeegui} <br> <br> }<br> }|Set-qaduser -usermustchangepassword $false</blockquote>by nate-n8 at 2013-04-10 08:50:21<blockquote>Try:<br><br>If($_.mailenabled -eq 'false')<br>-or-<br>If($_.mailenabled -like &quot;false&quot;)</blockquote>by ArtB0514 at 2013-04-10 09:05:09<blockquote>Please use the Code or powershell buttons as recommended in rule 1 at the top of this forum so that we can read your scriptblock easier. Do you have a specific question about your script, like how to resolve the first error message that is generated when you run it?<br><br>Just looking over the scriptblock without trying to run it, I see a number of (what I'll call) spelling errors:<br>[list]The Else clause is missing the opening &quot;{&quot;<br>&quot;eq&quot; is missing the initial &quot;-&quot;<br>&quot;False&quot; is either missing quote marks or the initial &quot;$&quot;<br>The foreach block is missing the closing &quot;}&quot;<br>There's an extraneous bactick at the end of your New-QADUser command[/list]<br>There is no object being fed to your final pipeline. You should probably move it to the end of the New-QADUser command.<br>Finally, your description says you want to create a mailbox, but you never do.</blockquote>by Candee at 2013-04-10 11:31:43<blockquote>Sorry about that. You're right - no mailboxes get created.<br>We have users that need AD accounts, but not mailboxes; and some that need both.<br>I'm trying to create the AD account for all the users, and then mailboxes just for those who require them.<br>My question is how to accomplish that.<br>thank you!<br><br>When I run this script with my csv, I get the list of users created, and then &quot;Email already exists&quot; at the bottom, no errors.<br><br> <code>Import-Csv $newusers |foreach {<br> $user = Get-QADUser -SamAccountName $_.SamAccountName<br> if&#40;$user -ne $Null&#41;<br> {<br> Write-host &quot;$User name already exists<br> &quot;<br> }<br> Else<br> {#create the new users<br> New-qaduser -parentcontainer $OU -name $
-samaccountname $
.samaccountname <br> -displayname $_.displayname
-givenname $.preferredfirstname<br> -sn $_.lastnamepreferred
-userPassword $pass <br> -company $_.Company
-department $
.department -title $.businesscardtitle -telephonenumber $.telephone <br> -city $ -postalcode $ -state $_.state
-streetaddress $.street -manager $.manager <br> -oa @{ipphone=$_.ipphone;mobile=$;employeeid=$_.employeeid;employeenumber=$_.employeegui}


}|Set-qaduser -usermustchangepassword $false

if($.mailenabled -eq ‘true’)
{#create the mailboxes
get-qaduser |New-mailbox -database $userdb <br> -userprincipalname $

write-host “Email already exists”
by ArtB0514 at 2013-04-10 12:23:43
Try this. I moved things around somewhat to try and keep all the tasks around the new user together and to make it a bit clearer which things apply to the new user and which to the user in the CSV. I also moved the lines around the scriptblock curly braces to fit my personal coding style. Feel free to not do that. Also, I didn’t test this, so it may have some errors. I changed $OU to $.OU, assuming that it came from the CSV file because it wasn’t defined elsewhere. Ditto for changing $pass to $.pass and $userdb to $.userdb.

Import-Csv $newusers | foreach {
$user = Get-QADUser -SamAccountName $.SamAccountName
if($user -ne $Null) {
“$($ already exists”
} Else {
"Creating a new user account for $($
$NewUser = New-QADUser -parentcontainer $.OU -name $.name <br> -samaccountname $_.samaccountname
-displayname $.displayname <br> -givenname $_.preferredfirstname
-sn $
.lastnamepreferred <br> -userPassword $_.pass
-company $.Company <br> -department $_.department -title $_.businesscardtitle -telephonenumber $_.telephone
-city $
.city -postalcode $.zip -state $.state <br> -streetaddress $_.street -manager $_.manager
-oa @{ipphone=$.ipphone;mobile=$.mobile;employeeid=$.employeeid;employeenumber=$.employeegui}
$NewUser | Set-qaduser -usermustchangepassword $false
if($.mailenabled -eq ‘true’) {
“Creating a mailbox for $($NewUser.Name)”
$NewUser | New-Mailbox -database $
.userdb -userprincipalname $.email
} else {
“Email already exists”
} # end of mailenabled
} # end of create new user
by Candee at 2013-04-10 12:43:53
Excellent - thank you!
Unfortunately, I’m getting an error:

Creating a new user account for TestCandee1

New-QADUser : A positional parameter cannot be found that accepts argument ‘$null’.
At C:\temp\scripts\create_mailbox1.ps1:33 char:35
+ $NewUser = New-qaduser <<<< -parentcontainer $
.OU -name $.name <br> + CategoryInfo : InvalidArgument: (:) [New-QADUser], ParameterBindingException<br> + FullyQualifiedErrorId : PositionalParameterNotFound,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.New<br> UserCmdlet<br><br>Email already exists</blockquote>by ArtB0514 at 2013-04-10 13:03:12<blockquote>Remember, I changed $OU to $_.OU assuming that it came from the CSV file. If it didn't, then you need to change it back. Same with $_.pass and $_.userdb.</blockquote>by Candee at 2013-04-10 13:12:39<blockquote>I did; I even tried adding the OU to the CSV and leaving it the way you had it.<br>I still get the same error. And I do have a 'name' column in the csv.<br>Thanks again for helping!</blockquote>by ArtB0514 at 2013-04-10 13:41:59<blockquote>Well, the error message says that one of the parameters is getting fed NULL, when it expects something else. Try this to see what the CSV file actually has and match the names to the parameters in the New-QADUser call.<br><code>&#40;Import-Csv $newusers&#41;[0] | Format-List *</code></blockquote>by Candee at 2013-04-11 05:29:55<blockquote>Thank you!<br>You were right of course - the csv had mailenable instead of mailenabled.<br>I continue to get that same error, though; until I remove this line:<br><code>$Newuser |Set-qaduser -usermustchangepassword $false</code><br>Then I get a different error (below) even if I sign on as my admin account. <br>Does it need to be run from the server?<br>Thanks again!<br><br>The &quot;RemovedMailbox&quot; parameter can't be used on the &quot;New-Mailbox&quot; cmdlet because it isn't present in the role definitio<br>n for the current user. Check the management roles assigned to you, and try again.<br> + CategoryInfo : PermissionDenied: (:) [New-Mailbox], CmdletAccessDeniedException<br> + FullyQualifiedErrorId : 97EEBD4D,Microsoft.Exchange.Management.RecipientTasks.NewMailbox</blockquote>by Candee at 2013-04-11 06:08:57<blockquote>I thought that could have been because I've been using the same list of users to test; so I renamed all the user accounts in the csv to something totally different; but I still get that error.<br>Frustrating!</blockquote>by ArtB0514 at 2013-04-11 06:38:28<blockquote>Back to this issue:<br><code>$Newuser |Set-qaduser -usermustchangepassword $false</code><br>Sorry, I just wasn't paying close enough attention to this when I was reformatting the code. Sometimes parameters that take Boolean arguments want a different syntax than normal (and I don't know why). This doesn't seem to be documented anywhere that I can find. But this syntax might work better in this case:<br><code>$Newuser | Set-qaduser -UserMustChangePassword]</code></blockquote>by Candee at 2013-04-11 06:46:37<blockquote>Not a problem - I appreciate the help.<br>This is what I get when I change that line:<br><br>Creating a new user account for TestCandee4<br>New-QADUser : A positional parameter cannot be found that accepts argument '$null'.<br>At C:\Temp\SCRIPTS\create_mailbox1.ps1:33 char:35<br>+ $NewUser = New-qaduser &lt;&lt;&lt;&lt;
+ CategoryInfo : InvalidArgument: (:slight_smile: [New-QADUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.New

Creating a mailbox for
The input object cannot be bound to any parameters for the command either because the command does not take pipeline in
put or the input and its properties do not match any of the parameters that take pipeline input.
+ CategoryInfo : InvalidArgument: (:slight_smile: [New-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,New-Mailbox
by ArtB0514 at 2013-04-11 07:42:48
Based on the errors, I went back and reread the help for the new-mailbox command and realized that it creates both the mailbox and the user account. That changes the way I’d go after processing this. Instead of creating the user and then adding a mailbox to it, I’d use SWITCH based on the mailboxenable parameter in the CSV file and do it this way. Again, not tested, so there will probably be more debugging to perform…
Import-Csv $newusers | foreach {
$user = Get-QADUser -SamAccountName $
if($user -eq $Null) {
“$($ already exists”
} Else {
Switch ($.mailboxenable) {
$false {"Creating a new user account for $($
New-QADUser -parentcontainer $.OU -name $.name <br> -samaccountname $_.samaccountname -displayname $_.displayname
-givenname $.preferredfirstname -sn $.lastnamepreferred <br> -userPassword $_.pass -company $_.Company -department $_.department
-title $.businesscardtitle -telephonenumber $.telephone <br> -city $ -postalcode $ -state $_.state
-streetaddress $.street -manager $.manager <br> -oa @{ipphone=$_.ipphone;mobile=$;employeeid=$_.employeeid;employeenumber=$_.employeegui}<br> Set-qaduser -Identity $_.SamAccountName -usermustchangepassword $false<br> Break}<br> $true {&quot;Creating a user amd mailbox for $&#40;$_.Name&#41;&quot;<br> $Mbx = New-Mailbox -UserPrincipalName $ -Database $_.userdb -Name $_.Name
-OrganizationalUnit $.OU -Password $.pass -FirstName $.preferredfirstname <br> -LastName $_.lastnamepreferred -DisplayName $_.displayname -Phone $_.telephone
-ResetPasswordOnNextLogon $false
Set-QADUser-Identity $Mbx.SamAccountName -Company $
.Company -Department $.Department <br> -Title $_.BusinessCardTitle -City $_.City -PostalCode $ -StateOrProvince $_.State
-StreetAddress $
.street -Manager $.manager <br> -ObjectAttributes @{ipphone=$_.ipphone;mobile=$;employeeid=$_.employeeid;employeenumber=$_.employeegui}<br> Break}<br> } # End Switch<br> } # end of else create new user<br>}</code></blockquote>by Candee at 2013-04-11 08:29:18<blockquote>This is light years ahead of me - I couldn't get it to work.<br>I did, however, get the other script to work, using enable-mailbox.<br>Thank you, thank you, thank you!!<br><br>The next thing (there's always a next thing) is to add acheck to see if the email address is used.<br><br><code>Import-Csv $newusers |foreach {<br> $user = Get-QADUser -SamAccountName $_.SamAccountName<br> if&#40;$user -ne $Null&#41; {<br> &quot;$&#40;$; already exists&quot;<br> } Else {<br> &quot;Creating a new user account for $&#40;$_.Name&#41;&quot;<br> <br> $NewUser = New-qaduser
-parentcontainer $OU -name $
.name <br> -samaccountname $_.samaccountname
-displayname $.displayname <br> -givenname $_.preferredfirstname
-sn $
.lastnamepreferred <br> -userPassword $pass
-company $.Company <br> -department $_.department -title $_.businesscardtitle -telephonenumber $_.telephone
-city $
.city -postalcode $.zip -state $.state <br> -streetaddress $_.street -manager $_.manager
-oa @{ipphone=$.ipphone;mobile=$.mobile;employeeid=$.employeeid;employeenumber=$.employeegui} <br> # $Newuser |Set-qaduser -usermustchangepassword:$false <br> <br> start-sleep -s 10<br> <br> if&#40;$_.mailenabled -eq &#39;true&#39;&#41; {<br> &quot;Creating a mailbox for $&#40;$;&quot;<br> $Newuser |foreach-object {enable-mailbox -identity $newuser.dn -database $userdb}
}Else {
“User does not require Email”
}#end of mailenabled

}#end of create new user
by ArtB0514 at 2013-04-11 11:23:51
Something like this?

If ($_.mailenabled -eq ‘true’ -and -not (Get-Mailbox $NewUser.SamAccountName))
by Candee at 2013-04-11 12:21:29
nice! Thanks!
I think it will work; I’ll do some more testing tomorrow.
Although can I use that and still get separate statements?
“Email not required” or “Email address already exists”?
Thanks again for all your help!