export new user data to csv

Welcome Forums General PowerShell Q&A export new user data to csv

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
    Posts
  • #6545

    Member
    Points: 0
    Rank: Member

    by Candee at 2013-04-16 08:56:17

    I have a script that imports new users from a csv; creates their username, upn; then creates the users & mailboxes.
    I need a way to export the updated information – specifically the displayname, SAM, UPN, and the employeenumber
    I've tried:
    $newusers |select name,displayname,email |export-csv "C]
    but I can't seem to get it right.
    Any insight is appreciated.
    Here is the script I'm using (many thanks to ArtB0514!)

    Import-Csv $newusers |foreach {
    $preferredfirstname = $_.preferredfirstname
    $lastnamepreferred = $_.lastnamepreferred
    $SAM = $preferredfirstname.substring(0,1)+$lastnamepreferred
    $UPN = $preferredfirstname +"."+$lastnamepreferred+$DNSROOT
    $Displayname = $lastnamepreferred +" "+$preferredfirstname
    $Email = $UPN
    $testemail = get-recipient -identity $email -ErrorAction SilentlyContinue
    $user = Get-QADUser -SamAccountName $SAM

    if($user -ne $Null) {
    "
    **************************************************************************
    $($User.name) already exists -
    *****THIS USER NEEDS TO BE MANUALLY CREATED*****
    "

    } Elseif ($testemail -ne $Null) {
    "
    **************************************************************************
    $($DISPLAYNAME) Email address already exists
    *****THIS MAILBOX NEEDS TO BE MANUALLY CREATED*****
    "

    }ELSE{
    "
    **************************************************************************
    Creating a new user account for $($DISPLAYNAME)"

    $NewUser = New-qaduser -name $SAM `
    -parentcontainer $OU `
    -userprincipalname $UPN `
    -samaccountname $SAM `
    -displayname $displayname `
    -mail $email `
    -givenname $_.preferredfirstname`
    -sn $_.lastnamepreferred `
    -userPassword $pass `
    -company $_.Company `
    -department $_.department -title $_.businesscardtitle -telephonenumber $_.telephone `
    -city $_.city -postalcode $_.zip -state $_.state `
    -streetaddress $_.street -manager $_.manager `
    -oa @{ipphone=$_.ipphone;mobile=$_.mobile;employeeid=$_.employeeid;employeenumber=$_.employeegui} `

    start-sleep -s 10

    if($_.mailenabled -eq 'true') {
    "Creating a mailbox for $($Newuser.name)"
    $Newuser |foreach-object {enable-mailbox -identity $newuser.email -database $userdb } `
    }Else {
    "User does not require Email
    "

    }#end of mailenabled

    }#end of create new user
    }

    by JeffH at 2013-04-16 10:02:05

    To me this looks like $newusers is the path to a CSV file. Or do you mean to $newuser without the S?

    by ArtB0514 at 2013-04-16 10:53:48

    A couple of other thoughts...
    Consider using New-Mailbox because it will create the user account and mailbox in a single command and you won't need to waste time sleepling.
    You might want to create a new variable [such as $CreatedUsers = @()] at the beginning of your script, then add the new user account to it after it has been created [$CreatedUsers += $NewUser] and export the $CreatedUsers array to the csv file.

    by Candee at 2013-04-16 11:31:08

    Thanks!
    I tried using "switch" in the script, but can't get it to work.
    This one works the way I want it to, although it's kludgy.
    I will try the new variable.

    by Candee at 2013-04-16 11:44:07

    Thank you! It's working!

    by mandy_ADM at 2013-05-01 03:32:22

    thats the one i'm looking for.

    @candy Could you pls be so kind and post the working script here. Thank you so much

    mandy

The topic ‘export new user data to csv’ is closed to new replies.