Efficiently Produce Comma-Separated Strings

by Jan 19, 2018

Here is a super simple approach to create a list of quoted strings:

& { "'$($args -join "','")'" } hello this is a test

Here is the result:

 
'hello','this','is','a','test' 

This approach effectively makes use of PowerShell’s “Command Mode” where literals are treated as arguments. You could even pipe this to Set-Clipboard and then paste it back into your code. This is much easier than having to manually put quotes around each string.

 
PS> & { "'$($args -join "','")'" } hello this is a test  | Set-ClipBoard

PS> Get-ClipBoard
'hello','this','is','a','test'  

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu

Twitter This Tip! ReTweet this Tip!