Powershell Script to Export Contacts Note

This topic contains 9 replies, has 3 voices, and was last updated by Profile photo of Mike Pua Mike Pua 5 months, 2 weeks ago.

  • Author
    Posts
  • #18012
    Profile photo of Mark DiPofi
    Mark DiPofi
    Participant

    I have a script that will export outlook contacts. It seems that I am unable to export the notes field.

    $TempPath="$env:Userprofile\appdata\local\microsoft\outlook\"
    $strname=$env:username
    #New-Item c:\myscripts\output -type directory
    New-Item \\x.x.x.x\backupemail\users\$strname -ItemType directory
    New-Item \\x.x.x.x\backupemail\users\$strname\contacts -ItemType directory
    $Outlook=NEW-OBJECT –comobject Outlook.Application

    $Contacts=$Outlook.session.GetDefaultFolder(10).items

    ## $Contacts | Format-Table FullName,CompanyName,JobTitle,FileAs,Email1Address,WebPage,IMAddress,BusinessTelephoneNumber,BusinessFaxNumber,MobileTelephoneNumber,BusinessAddress,Notes

    $properties = 'FullName','CompanyName','JobTitle','FileAs','Email1Address','WebPage','IMAddress','BusinessTelephoneNumber','BusinessFaxNumber','BusinessAddress','MobileTelephoneNumber','HomeTelephoneNumber','Notes'
    $Contacts |
    Select-Object -Property $properties |
    Export-Csv -NoTypeInformation -Path \\x.x.x.x\backupemail\users\$strname\contacts\contacts.csv

    Thank you in advance

  • #18017
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    If you did a Select *, you should see all of the attributes and figure out which one is "Notes". On my client, it appears to be 'Body'. So, add [b]Body[/b] to your Property array

  • #18024
    Profile photo of Mark DiPofi
    Mark DiPofi
    Participant

    Rob,
    Thank you for the information. I am going to have to play with the Select * and figure out that works so I can get a better fell of things. I am going to assume this will also help with the Calendar if I use a Select * and figure out what fields I need for Properties.
    Tested and it worked like a charm.

  • #18025
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You can additionally use Get-Member to see what properties are available. However, in this case I wanted to look for my test text in the Note field, so I used Select * to return all properties and values. Another tip is if you have a lot of contacts and just want to see one, then you can use Select * -First 1 to only return the first record and review the properties.

    $contacts | Get-Member -MemberType Property
    $contacts | Select * -First 1

  • #18033
    Profile photo of Mark DiPofi
    Mark DiPofi
    Participant

    Ok now I am confused some. I tried both command to see what the output would look like.
    When I do ( $contacts | Get-Member -MemberType Property) I see a bunch of fields vs
    $contacts | Select * -First 1 (most of these fields are different than the first command).

    May I ask what I am not understanding?

    Thank you

  • #43979
    Profile photo of Mike Pua
    Mike Pua
    Participant

    Hi Mark,

    Sorry to revive an old thread but I have a quick question about your script. Everything works great and I am able to export my contacts to a CSV, the only issue I am having is with the headers of the exported file.

    Is there anyway to customize the headers when they are exported? Currently when importing back into Outlook, certain fields have to be manually mapped (ie. "ComanyName" isn't mapped because it is looking for "Company").

    Thanks in advance!
    Mike

  • #43997
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Say you have a CSV like so:

    User, Computer, Company
    jsmith1,cmp123,powershell.org
    tfranklin,cmp452,powershell.org
    rsimmers,cmp632,powershell.org
    

    You could do either this (define a new header and skip the original):

    Import-CSV C:\Users\rob\desktop\test.csv -Header UserName, ComputerName, CompanyName | Select -Skip 1
    

    or this (calculated expression) :

    Import-CSV C:\Users\rob\desktop\test.csv | Select User, Computer, @{Name="CompanyName";Expression={$_.Company}}
    
  • #44009
    Profile photo of Mike Pua
    Mike Pua
    Participant

    Thanks Rob, so it looks like that imported into powershell and displayed all the info and headers correctly (within powershell). How can I get that into another csv file or better yet, do you know how I could import it straight to Outlook?

  • #44031
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Exporting is simply calling Export-CSV:

    $contacts = Import-CSV C:\Users\rob\desktop\test.csv -Header UserName, ComputerName, CompanyName | Select -Skip 1
    $contacts | Export-CSV C:\Users\Rob\Desktop\updated_contacts.csv -NoTypeInformation
    

    If you are looking at importing, you should look at scripts like this: https://gallery.technet.microsoft.com/office/Using-Powershell-to-import-14bef4b8

    You'll notice that they are doing a mapping of columns like Company to CompanyName, so you don't need to bother with column changes.

  • #44123
    Profile photo of Mike Pua
    Mike Pua
    Participant

    Thanks Rob, import/export script worked. The link you provided to import the contacts refers to importing into Exchange though. I'm just looking to import into the Outlook client. I have a couple examples of importing to Outlook, hopefully I can get it working.

    Thanks again!

You must be logged in to reply to this topic.