Skip to content

Instantly share code, notes, and snippets.

@davehull
Created March 13, 2017 15:48
Show Gist options
  • Save davehull/8e5b009883b3316a66e7bd1d063fd721 to your computer and use it in GitHub Desktop.
Save davehull/8e5b009883b3316a66e7bd1d063fd721 to your computer and use it in GitHub Desktop.
ConvertTo-DelimiterSeparatedValues
function ConvertTo-DelimiterSeparatedValues {
<#
This function is like ConverTo-CSV but with
support for multi-character delimiters. The
function will return noteproperty names as
a header row.
#>
param(
[Parameter(Mandatory=$True,ValueFromPipeLine=$True,Position=0)]
[pscustomobject[]]$arrObject,
[Parameter(Mandatory=$False,Position=1)]
[String]$strDelimiter=":|"
)
# Create a header row from the names of NoteProperties
$header = @()
$header += ($arrObject | Get-Member -Type NoteProperty | Select-Object -ExpandProperty Name)
# return the delimited header
$header -join $strDelimiter
# return delimited rows of data
(
$arrObject | ForEach-Object {
$arrObject_ = $_ # Name the automatic variable, we need it in the inner loop
( $header | ForEach-Object {
$arrObject_.$_
} ) -join $strDelimiter
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment