Created
October 18, 2019 12:20
-
-
Save randomnote1/5ffd9393cb6b131bd633c87b74e23a8a to your computer and use it in GitHub Desktop.
Convert a CSV of contact information to a VCF file for the Light Phone II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Create a vCard VCF file from a CSV. | |
.DESCRIPTION | |
Create a vCard VCF file from a CSV containing contacts. | |
.PARAMETER File | |
The CSV file containing the contacts. | |
.PARAMETER OutPath | |
The directory to write the VCF file. | |
.PARAMETER OutFileName | |
The name of the VCF file. Default is "light-contacts.vcf". | |
.EXAMPLE | |
.\ConvertTo-LightVcf.ps1 | |
.EXAMPLE | |
.\ConvertTo-LightVcf.ps1 -File C:\Users\randomnote1\Downloads\contacts.csv -OutPath C:\Users\randomnote1\Downloads | |
#> | |
[CmdletBinding()] | |
param | |
( | |
[Parameter()] | |
[System.IO.FileInfo] | |
$File, | |
[Parameter()] | |
[System.IO.DirectoryInfo] | |
$OutPath, | |
[Parameter()] | |
[System.IO.FileInfo] | |
$OutFileName = 'light-contacs.vcf' | |
) | |
# Load the Windows Forms assembly | |
Add-Type -AssemblyName System.Windows.Forms | |
# If no file was provided | |
if ( -not $File ) | |
{ | |
Write-Warning -Message 'Select the CSV file which contains your contacts. The file selection dialogue may be behind this window.' | |
# Prompt the user to select the contacts CSV file | |
$fileBrowser = New-Object -TypeName System.Windows.Forms.OpenFileDialog -Property @{ | |
Filter = 'CSV (*.csv)|*.csv' | |
InitialDirectory = [Environment]::GetFolderPath('Desktop') | |
} | |
$fileBrowser.ShowDialog() > $null | |
if ( $fileBrowser.FileName ) | |
{ | |
$File = [System.IO.FileInfo] $fileBrowser.FileName | |
} | |
else | |
{ | |
throw 'No file was selected.' | |
} | |
} | |
# If no output folder was specified | |
if ( -not $OutPath ) | |
{ | |
Write-Warning -Message 'Select the directory to save the VCF file. The folder selection dialogue may be behind this window.' | |
# Prompt the user to select the output path | |
$folderBrowser = New-Object -TypeName System.Windows.Forms.FolderBrowserDialog -Property @{ | |
ShowNewFolderButton = $true | |
} | |
$folderBrowser.ShowDialog() > $null | |
if ( $folderBrowser.SelectedPath ) | |
{ | |
$OutPath = [System.IO.DirectoryInfo] $folderBrowser.SelectedPath | |
} | |
else | |
{ | |
throw 'No output directory was selected.' | |
} | |
} | |
# Import the contacts from the specified CSV | |
$contactsCsv = Get-Content -Path $File | ConvertFrom-Csv | |
# Get the phone number properties | |
$phoneNumberProperties = $contactsCsv | | |
Get-Member -Name *Phone* -MemberType NoteProperty | | |
Select-Object -ExpandProperty Name | |
# Get the mobile phone properties | |
$mobilePhoneProperties = $phoneNumberProperties | Where-Object -FilterScript { $_ -match 'mobile|cell' } | |
# Get the home phone properties | |
$homePhoneProperties = $phoneNumberProperties | Where-Object -FilterScript { $_ -match 'home' } | |
# Get the business phone properties | |
$businessPhoneProperties = $phoneNumberProperties | Where-Object -FilterScript { $_ -match 'business|company' } | |
# Create an array to store the vCards in | |
$vCards = @() | |
foreach ( $contact in $contactsCsv ) | |
{ | |
# Determine if this is a person or a business | |
if ( [System.String]::IsNullOrEmpty($contact.'First Name') -and [System.String]::IsNullOrEmpty($contact.'LastName') ) | |
{ | |
$firstName = $contact.Company | |
} | |
else | |
{ | |
$firstName = $contact.'First Name' | |
} | |
# If a mobile phone number exists | |
if ( $mobilePhoneProperties | Where-Object -FilterScript { -not [System.String]::IsNullOrEmpty($contact.$_) } ) | |
{ | |
# Get one of the mobile numbers | |
$phoneNumber = $mobilePhoneProperties | | |
Where-Object -FilterScript { -not [System.String]::IsNullOrEmpty($contact.$_) } | | |
ForEach-Object -Process { $contact.$_ } | | |
Select-Object -First 1 | |
} | |
# Otherwise, if a home phone number exists | |
elseif ( $homePhoneProperties | Where-Object -FilterScript { -not [System.String]::IsNullOrEmpty($contact.$_) } ) | |
{ | |
# Get one of the home numbers | |
$phoneNumber = $homePhoneProperties | | |
Where-Object -FilterScript { -not [System.String]::IsNullOrEmpty($contact.$_) } | | |
ForEach-Object -Process { $contact.$_ } | | |
Select-Object -First 1 | |
} | |
# Otherwise, if a business phone number exists | |
elseif ( $businessPhoneProperties | Where-Object -FilterScript { -not [System.String]::IsNullOrEmpty($contact.$_) } ) | |
{ | |
# Get one of the business phone numbers | |
$phoneNumber = $businessPhoneProperties | | |
Where-Object -FilterScript { -not [System.String]::IsNullOrEmpty($contact.$_) } | | |
ForEach-Object -Process { $contact.$_ } | | |
Select-Object -First 1 | |
} | |
else | |
{ | |
# Get a number from the remaining phone number fields | |
$phoneNumber = $phoneNumberProperties | | |
Where-Object -FilterScript { -not [System.String]::IsNullOrEmpty($contact.$_) } | | |
ForEach-Object -Process { $contact.$_ } | | |
Select-Object -First 1 | |
} | |
# Remove all formatting from the phone number | |
$phoneNumber = $phoneNumber -replace '\(|\)|-|\s|\+|','' | |
# Build the vCard using a string builder | |
$sb = New-Object -TypeName System.Text.StringBuilder | |
$sb.AppendLine('BEGIN:VCARD') > $null | |
$sb.AppendLine('VERSION:3.0') > $null | |
$sb.AppendLine("N:$($contact.'Last Name');$firstName") > $null | |
$sb.AppendLine("FN:$firstName $($contact.'Last Name')") > $null | |
$sb.AppendLine("TEL;TYPE=cell:$phoneNumber") > $null | |
$sb.AppendLine('END:VCARD') > $null | |
# Add the vCard to the vCard array | |
$vCards += $sb.ToString() | |
} | |
# Build the path to the outfile | |
$outFile = Join-Path -Path $OutPath -ChildPath $OutFileName | |
# Export the vCards to a file | |
$vCards | Out-File -FilePath $outFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment