Blog Home  Home Feed your aggregator (RSS 2.0)  
.Net Jonesie - PowerShell Love – Converting a comma delimited string to an array
A simple programmers blog
 
# Friday, July 17, 2009

PowerShell takes a while to learn – especially if you have an aversion to manuals as I do – I prefer to learn on the fly.  So, when I wanted to convert a variable containing a comma delimited string into an array I was lost.  After a bit of playing and ‘binging’  I found that it was actually very very easy:

# start with a string
$roles = "db_owner"

# now it’s an array of 1 element
$roles = ,$roles

Sweet!

Friday, July 17, 2009 11:33:27 AM (New Zealand Standard Time, UTC+12:00)  #    Comments [1]   General | PowerShell  | 
Saturday, July 18, 2009 3:48:44 AM (New Zealand Standard Time, UTC+12:00)
Good shortcut.

Another way is

$roles = @($roles)
or
$roles = @("db_owner")

The array notation is useful when you want to ensure you have an array, especially when items are returned from a function.

$roles = @(Get-Roles)
Comments are closed.
Copyright © 2010 Peter G Jones. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: