-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-CSV-Headers.ps1
More file actions
66 lines (52 loc) · 1.78 KB
/
Get-CSV-Headers.ps1
File metadata and controls
66 lines (52 loc) · 1.78 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
<#
.SYNOPSIS
Gets the header information of the specified CSV file.
.DESCRIPTION
Gets the header information of the specified CSV file.
.NOTES
Author: Jonathan Panning <jpann [at] impostr-labs.com>
Version: 1.0
Date Updated: 12-06-2023
.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
Returns an object containing the file's header information.
.EXAMPLE
PS> .\Get-CSV-Headers.ps1 -Path "..\..\MockData\MOCK_PEOPLE_DATA.csv"
ColumnCount Columns Header File
----------- ------- ------ ----
5 {id, first_name, last_name, email...} id,first_name,last_name,email,description MOCK_PEOPLE_DATA.csv
.EXAMPLE
PS> Get-ChildItem -Path '..\..\MockData\*.csv' | .\Get-CSV-Headers.ps1
#>
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject[]])]
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
$header = Get-Content $Path -TotalCount 1 | Select -First 1
$headerColumns = $header.Split($Delimiter)
$headerData = New-Object PSObject -Property @{
'File' = Split-Path $Path -Leaf;
'ColumnCount' = $headerColumns.Count
'Header' = $header
'Columns' = $headerColumns
}
$headerData | ForEach {[PSCustomObject]$_} | Format-Table -AutoSize | Write-Output