# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009 # # NAME: OUT-CSV.ps1 # # AUTHOR: Jeffery Hicks , SAPIEN Technologies, Inc. # DATE : 5/15/2009 # # COMMENT: Create a comma separated string from input. The function will only # create a string from a single property. The default is the NAME property of # any pipelined object. But you can use -Property to select a different object # property. # The default delimiter is a comma, but you can # specify something else with -delimiter # #examples # get-service | where {$_.status -eq "running"} | out-csv # Get-Process | where {$_.workingset -gt 10MB} | out-csv -delimiter ";" # get-eventlog -list | out-csv -property log # DISCLAIMER AND WARNING: # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY # KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. # TEST THOROUGHLY IN A NON-PRODUCTION ENVIRONMENT. IF YOU DON'T KNOW WHAT THIS # SCRIPT WILL DO...DO NOT RUN IT! # ============================================================================================== Function out-csv { Param([string]$property="Name",[string]$delimiter=",") BEGIN { #initialize placeholder array $csv=@() } PROCESS { #force each item to be a string and trim any spaces $item=($_.$property | out-string).Trim() #add item to the array $csv+=$item } END { $OFS=$delimiter #send the comma separated string into the pipeline write ($csv -as [string]) } }