-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsv-AddDoubleQuotes.ps1
105 lines (82 loc) · 2.93 KB
/
Csv-AddDoubleQuotes.ps1
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
99
100
101
102
103
104
105
<#
.SYNOPSIS
This script will import a CSV file and convert it to using qouted fields.
.DESCRIPTION
This script will import a CSV file and convert it to using qouted fields.
You must update the columns specified in the script manually.
.NOTES
Author: Jonathan Panning <jpann [at] impostr-labs.com>
Filename: Csv-AddDoubleQuotes.ps1
Created on: 12-06-2023
Version: 1.0
Last updated: 12-06-2023
.PARAMETER Path
Specifies the input file.
.PARAMETER Output
Specifies the output file.
.PARAMETER Delimiter
Specifies the CSV column delimter. Default: ,
.EXAMPLE
PS> Import-Module .\Csv-AddDoubleQuotes.ps1 -Force
PS> Add-CsvDoubleQuotes -Path .\MyCSV.csv -Output .\Updated.csv
.EXAMPLE
PS> Import-Module .\Csv-AddDoubleQuotes.ps1 -Force
PS> Add-CsvDoubleQuotes -Path ..\..\MockData\MOCK_COLORS.csv -Output $HOME\MOCK_COLORS_Quoted.csv
#>
#Requires -Version 7.0
function Add-CsvDoubleQuotes {
<#
.SYNOPSIS
This script will import a CSV file and convert it to using qouted fields.
.DESCRIPTION
This script will import a CSV file and convert it to using qouted fields.
You must update the columns specified in the script manually.
.NOTES
Author: Jonathan Panning <jpann [at] impostr-labs.com>
Filename: Csv-AddDoubleQuotes.ps1
Created on: 12-06-2023
Version: 1.0
Last updated: 12-06-2023
.PARAMETER Path
Specifies the input file.
.PARAMETER Output
Specifies the output file.
.PARAMETER Delimiter
Specifies the CSV column delimter. Default: ,
.EXAMPLE
PS> Import-Module .\Csv-AddDoubleQuotes.ps1 -Force
PS> Add-CsvDoubleQuotes -Path .\MyCSV.csv -Output .\Updated.csv
.EXAMPLE
PS> Import-Module .\Csv-AddDoubleQuotes.ps1 -Force
PS> Add-CsvDoubleQuotes -Path ..\MockData\MOCK_COLORS.csv -Output $HOME\MOCK_COLORS_Quoted.csv
#>
[CmdletBinding()]
Param(
# Specifies the input file.
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[alias("s")]
[Alias('FullName')]
[string]
$Path,
# Specifies the output file.
[parameter(Mandatory=$true)]
[alias("o")]
[string]
$Output,
[parameter(Mandatory=$false)]
[string]
$Delimiter = ","
)
BEGIN {
}
PROCESS {
$content = Import-Csv -Path $Path -Delimiter $Delimiter
$columns = ($content[0].PSObject.Properties).Name
[String[]]$columnsList = @()
$columns | Foreach { $columnsList += $_}
# Could also do it using -UseQuotes Always, which will quote everything...
#$content | Export-Csv -Path $Output -NoTypeInformation -UseQuotes Always
$content | Export-Csv -QuoteFields $columnsList -Path $Output -NoTypeInformation
}
}