FormatMarkdown.psm1
function Format-Markdown { <# .Author Trevor Sullivan <trevor@trevorsullivan.net> http://trevorsullivan.net .Synopsis Formats an array of objects as a Markdown table. .Parameter Properties The -Properties parameter accepts an array of System.String objects, which represents the properties that will be retrieved from the input objects. .Parameter InputObject The -InputObject accepts an array of arbitrary objects. The objects should all be of the same type. If the input objects are of different types, then the output from this command will be unpredictable. .Example Get-Process -Name *b* | Format-Markdown -Properties Id,Name,WorkingSet; .Example Format-Markdown -Properties Name,Status,DisplayName -InputObject (Get-Service); #> [CmdletBinding()] param ( [Parameter(ValueFromPipeline = $true, Mandatory = $true)] [System.Object[]] $InputObject , [string[]] $Properties = '*' ) begin { } process { $Params = $PSBoundParameters; foreach ($Item in $InputObject) { $MDString += "`n"; foreach ($Property in ($Item | Get-Member -MemberType Property,NoteProperty,AliasProperty,ScriptProperty -Name $Properties)) { Write-Verbose -Message ('Item is: {0}; Property name is: {1}' -f $Item.ToString(), $Property.Name); try { $PropValue = $null; $PropValue = $Item.$($Property.Name).ToString(); $MDString += '|{0}' -f $PropValue; } catch { Write-Verbose -Message ('Failed to obtain value for property: {0}' -f $Property.Name); } } $MDString += '|'; } } end { $PropList = $InputObject | Get-Member -MemberType Property,NoteProperty,AliasProperty,ScriptProperty -Name $Properties; $Header = ''; foreach ($Property in $PropList) { $Header += '|{0}' -f $Property.Name; } $Header += '|'; $Header += $PropList | ForEach-Object -Begin { "`r`n|" } -Process { '---|' } -End { '|'; }; $MDString = $MDString.Insert(0, $Header); Write-Output $MDString; } } ### Create an alias for the Format-Markdown command New-Alias -Name fmd -Value Format-Markdown -Description 'Formats an array of similar objects as a Markdown table.'; ### Export the command and alias explicitly Export-ModuleMember -Function Format-Markdown -Alias fmd; |