-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-CSV-MaxLengths.ps1
More file actions
79 lines (61 loc) · 2.06 KB
/
Get-CSV-MaxLengths.ps1
File metadata and controls
79 lines (61 loc) · 2.06 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
<#
.SYNOPSIS
Gets the max length of each column in the specified CSV file.
.DESCRIPTION
This gets the maximum column length of each column in the specified CSV file.
.NOTES
Author: Jonathan Panning <jpann [at] impostr-labs.com>
Version: 1.0
Date Updated: 12-06-2023
Version History:
- 1.0: Initial version.
.LINK
https://github.com/jpann/PowerShell
.PARAMETER Path
The path to the CSV file.
.PARAMETER Delimiter
The field delimiter. Default ','.
.INPUTS
System.String. Accepts objects piped with the property Path or FullName.
.OUTPUTS
Hashtable. Returns a hash table containing the column name and the maximum length.
.EXAMPLE
PS .\Get-CSV-MaxLengths.ps1 -Path "..\..\MockData\MOCK_PEOPLE_DATA.csv"
Name Value
---- -----
email 37
last_name 22
description 550
first_name 14
id 4
.EXAMPLE
PS> .\Get-CSV-MaxLengths.ps1 -Path "..\..\MockData\MOCK_PEOPLE_DATA.csv" | Export-Csv "C:\MOCK_PEOPLE_DATA_MaxColLengths.csv" -NoTypeInformation -Encoding UTF8 -Force
.EXAMPLE
PS> .\Get-CSV-MaxLengths.ps1 -Path "..\..\MockData\MOCK_PEOPLE_DATA.csv" | Out-GridView
#>
[CmdletBinding()]
[OutputType([Hashtable])]
Param(
[parameter(Mandatory=$true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Enter the path to CSV file.')]
[ValidateNotNullOrEmpty()]
[Alias('FullName')]
[string]
$Path,
[parameter(Mandatory=$false)]
[string]
$Delimiter = ","
)
cd $PSScriptRoot
$content = Import-Csv -Path $Path -Delimiter $Delimiter
# create an empty hash for column lengths
$columnLengths = @{}
# Process each column by name
foreach ($columnName in $(($csv | Get-Member -MemberType NoteProperty).Name))
{
$columnLengths[$columnName] = ($csv.$columnName | Measure-Object -Maximum -Property Length).Maximum
}
# Output column lengths
Write-Output $columnLengths