Last active
September 25, 2018 04:26
-
-
Save mklement0/17aa040ab85391749af7cf5a57b6e11b to your computer and use it in GitHub Desktop.
New-CsvSampleData: PowerShell function for automated creation of sample CSV data
This file contains hidden or 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
function New-CsvSampleData { | |
<# | |
.SYNOPSIS | |
Generates CSV sample data. | |
.DESCRIPTION | |
Generates simple CSV sample data as either a single multiline string or | |
an array of lines. | |
For now, the data-row field values are simply constructed as: | |
<columnName>Val<ndx> | |
where <ndx> is a sequence number starting at 1. | |
By default, 5 rows are generated; use -Count to modify. | |
.PARAMETER Columns | |
Either: | |
An array of explicit column names. | |
Or: | |
A single number indicating how many columns to generate in the form of | |
"col<ndx>", where <ndx> is a 1-based sequence number; e.g., for 3: | |
"col1","col2","col3" | |
.PARAMETER Count | |
The number of data rows to generate. Defaults to 5. | |
.PARAMETER Delimiter | |
The field delimiter (separator) to use; defaults to a comma. | |
Only a single char. is supported. | |
.PARAMETER Stream | |
Request output as an array of rows (lines). | |
By default, a single multiline string is output. | |
.PARAMETER Newline | |
The newline character [sequence] to use to separate output rows (applies only | |
if -Stream is not specified). | |
Defaults to the platform-native newline format. | |
Pass a literal sequence to override, namely "`n" for Unix-style newlines, and | |
"`r`n" for Windows-style newlines. | |
.NOTES | |
Requires PSv4+ | |
.EXAMPLE | |
New-CsvSampleData 3 2 | |
Generates a 3-column CSV with auto-generated columns names with 2 rows of | |
auto-generated values, which yields: | |
"col1","col2","col3" | |
"col1Val1","col2Val1","col3Val1" | |
"col1Val2","col2Val2","col3Val2" | |
.EXAMPLE | |
New-CsvSampleData foo, bar, baz 3 | |
Generates a multiline CSV string with columns 'foo', 'bar', and 'baz' and | |
3 data rows, which yields: | |
"foo","bar","baz" | |
"fooVal1","barVal1","bazVal1" | |
"fooVal2","barVal2","bazVal2" | |
"fooVal3","barVal3","bazVal3" | |
.EXAMPLE | |
New-CsvSampleData Id, Name 100 -Stream | Select-Object -First 2 | |
Generates CSV rows that, due to -Stream, are sent through the pipeline | |
one by one (a 2-element array of strings). | |
"Id","Name" | |
"IdVal1","NameVal1" | |
#> | |
[CmdletBinding(PositionalBinding=$False)] | |
param( | |
[parameter(Mandatory, Position=0, HelpMessage='Enter the column names')] | |
[string[]] $Columns | |
, | |
[parameter(Position=1)] | |
[ValidateRange(0, [int]::MaxValue)] | |
[int] $Count = 5 | |
, | |
[Alias('Separator')] | |
[char] $Delimiter = ',' # "delimiter" == separator: separates header and data-row values | |
, | |
[switch] $Stream # output an array of lines rather than a single string | |
, | |
[string] $Newline = [environment]::NewLine # n/a if -Stream is specified | |
) | |
# If only a single number was specified for -Columns, interpret it | |
# as the number of columns to auto-generate as "col<ndx>". | |
if ($Columns.Count -eq 1 -and ($numCols = $Columns[0] -as [int])) { | |
$Columns = 1..$numCols -replace '^', 'col' | |
} | |
# Synthesize a data-row template in the form of <colName>Val<ndx> field values. | |
$valTemplate = $(foreach ($col in $Columns) { | |
"`"${col}Val{0}`"" | |
}) -join $Delimiter | |
$headerRow = '"{0}"' -f ($Columns -join "`"$Delimiter`"") | |
$dataRows = $null | |
if ($Count) { | |
$dataRows = (1..$Count).ForEach({ $valTemplate -f $_ }) | |
} | |
if ($Stream) { # output an array of lines | |
$headerRow | |
$dataRows | |
} else { # output a single, multiline string. | |
# Note: We do NOT append a trailing newline, because PowerShell's > / Out-File / Set-Content | |
# do that on outputting to a file by default. | |
'{0}{1}{2}' -f $headerRow, ($Newline * ([bool] $dataRows)), ($dataRows -join $NewLine) | |
} | |
} | |
# If this script is invoked directly - as opposed to being dot-sourced in order | |
# to define the embedded function for later use - invoke the embedded function, | |
# relaying any arguments passed. | |
if (-not ($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq '')) { | |
New-CsvSampleData @Args | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment