Using a PowerShell Quote List Function to Work with an Array of Strings

Reblogged from Mississippi PowerShell User Group:

Recently, during one of our test user group meetings that Rohn and I had, I was using an example of storing multiple server names into an array to then work with them. This is how I would normally accomplish this task, that is unless I pulled the names directly from Active Directory:

$servers = 'server1', 'server2', 'server3', 'server4', 'server5', 'server6'
$servers

](https://mspsug.com/wp-content/uploads/2013/03/ql1.png)

Rohn showed me the way he has been accomplishing this sort of task which requires less typing:

$servers = 'server1,server2,server3,server4,server5,server6' -split ','
$servers

ql2.png

I tweaked Rohn’s example a little since I prefer to split on spaces, but it’s basically the same example:

$servers = 'server1 server2 server3 server4 server5 server6' -split ' '
$servers

](https://mspsug.com/wp-content/uploads/2013/03/ql3.png)

With further testing, I determined I could use the split parameter first and save a few more characters even though the tabbed expansion or intellisense in the ISE didn’t like this, it still worked:

$servers = -split 'server1 server2 server3 server4 server5 server6'
$servers

](https://mspsug.com/wp-content/uploads/2013/03/ql4.png)

Rohn said he recently learned about using a function called a quote list where you can define the function and then simply pass your items to it with no punctuation required:

function ql{$args}
$servers = ql server1 server2 server3 server4 server5 server6
$servers

](https://mspsug.com/wp-content/uploads/2013/03/ql5.png)

I decided to record a short video of this process:

[youtube=https://youtu.be/0uLB5iR1LzQ]

Don’t forget to join us on Tuesday, March 12th at 8:30pm Central Time for our user group meeting which is held online via Microsoft Lync. We’ll be giving away two ebook copies of the PowerShell Deep Dives book.

µ

Update: I asked Rohn to take a look at this post and see what he thought. He said he learned about the quote list function while reading the “Windows PowerShell in Action, Second Edition” book written by Bruce Payette.

µ