-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClean-PhoneNumbers.ps1
More file actions
98 lines (79 loc) · 2.94 KB
/
Copy pathClean-PhoneNumbers.ps1
File metadata and controls
98 lines (79 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<#
.SYNOPSIS
Cleans and formats phone numbers from CSV or TXT files into a standard format: (XXX) XXX-XXXX
.DESCRIPTION
This script reads phone numbers from a source CSV (with a 'Phone' column) or TXT file,
removes unwanted characters, and formats them consistently.
The cleaned numbers are exported into a new CSV or TXT file.
.PARAMETER InputFile
Path to the input CSV or TXT file.
- CSV must contain a column named "Phone"
- TXT should contain one phone number per line
.PARAMETER OutputFile
Path to save the cleaned output file.
- Can be a full file path (e.g., cleaned_contacts.csv)
- Or just a folder path (script will generate "cleaned_<inputname>.<ext>")
.EXAMPLE
# Clean a CSV and explicitly name the output
./Clean-PhoneNumbers.ps1 -InputFile "contacts.csv" -OutputFile "cleaned_contacts.csv"
.EXAMPLE
# Clean a TXT file and let script generate output file in a folder
./Clean-PhoneNumbers.ps1 -InputFile "numbers.txt" -OutputFile "C:\OutputFolder\"
.EXAMPLE
# Clean a TXT file, saving in same folder with "cleaned_" prefix
./Clean-PhoneNumbers.ps1 -InputFile ".\numbers.txt" -OutputFile ".\"
#>
param (
[Parameter(Mandatory = $true)]
[string]$InputFile,
[Parameter(Mandatory = $true)]
[string]$OutputFile
)
function Format-PhoneNumber {
param (
[string]$RawNumber
)
# Remove everything except digits
$digits = ($RawNumber -replace '\D', '')
# Ensure it has 10 digits (US-style phone)
if ($digits.Length -eq 10) {
return "({0}) {1}-{2}" -f $digits.Substring(0, 3), $digits.Substring(3, 3), $digits.Substring(6, 4)
}
else {
return $RawNumber # Return as-is if not valid
}
}
# --- Handle Output Path ---
if ((Test-Path $OutputFile) -and (Get-Item $OutputFile).PSIsContainer) {
# If output is a folder, auto-generate filename
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($InputFile)
$ext = [System.IO.Path]::GetExtension($InputFile)
$OutputFile = Join-Path $OutputFile ("cleaned_" + $baseName + $ext)
}
# --- Detect File Type ---
if ($InputFile.EndsWith(".csv")) {
Write-Host "📂 Processing CSV file: $InputFile"
$data = Import-Csv $InputFile
if (-not $data[0].PSObject.Properties.Name -contains "Phone") {
Write-Error "CSV must contain a column named 'Phone'"
exit 1
}
foreach ($row in $data) {
$row.Phone = Format-PhoneNumber $row.Phone
}
$data | Export-Csv -Path $OutputFile -NoTypeInformation
Write-Host "✅ Cleaned CSV saved to $OutputFile"
}
elseif ($InputFile.EndsWith(".txt")) {
Write-Host "📂 Processing TXT file: $InputFile"
$lines = Get-Content $InputFile
$cleaned = foreach ($line in $lines) {
Format-PhoneNumber $line
}
$cleaned | Out-File -FilePath $OutputFile -Encoding UTF8
Write-Host "✅ Cleaned TXT saved to $OutputFile"
}
else {
Write-Error "❌ Unsupported file type. Use CSV or TXT."
exit 1
}