Answered by:
Create an array with readability

Question
-
Hi guys,
Is there an easy to read way of creating an array without it spanning across a long line?
$Example = @('one','two','three','four','Five','one','two','three','four','Five','one','two','three','four','Five','one','two','three','four','Five') $Example
I need to delete a large list of shortcuts from all desktops. I am using the psappdeploytoolkit and it would be nice if they were somehow listed vertically rather than a long long long line :)
Sorry for the dumb question!
Richard Knight | Collection Refresh Manager | Automate detection rules for patch \ msp files | Twitter
Wednesday, September 18, 2019 7:22 AM
Answers
-
The comma is used as carriage return:
$Example = @('one','two','three','four','Five', 'one','two','three','four','Five', 'one','two','three','four','Five', 'one','two','three','four','Five')
Or you can use a backtick character:
$Example = @('one','two','three','four','Five', ` 'one','two','three','four','Five', ` 'one','two','three','four','Five', ` 'one','two','three','four','Five')
- Marked as answer by Richard.Knight Wednesday, September 18, 2019 12:32 PM
Wednesday, September 18, 2019 7:45 AM -
Much easier than al of that is just place all on separate lines.
$Example = 'one', 'two', 'three', 'four', 'Five'
\_(ツ)_/
- Marked as answer by Richard.Knight Wednesday, September 18, 2019 12:33 PM
Wednesday, September 18, 2019 7:52 AM -
This also elim9inates the need to manage quotes and commas:
$Example = @' one two three four five '@ -split "`n" $Example
\_(ツ)_/
- Edited by jrv Wednesday, September 18, 2019 7:55 AM
- Marked as answer by Richard.Knight Wednesday, September 18, 2019 12:33 PM
Wednesday, September 18, 2019 7:54 AM -
The @() isn't necessary. The comma operator makes the array. I also find using echo less tedious. I don't have to quote everything.
$Example = echo one two three four Five one two three four Five one,
two three four Five one two three four Five
One element array example:
$a = ,1
- Marked as answer by Richard.Knight Thursday, September 19, 2019 3:02 PM
- Edited by JS2010 Thursday, September 19, 2019 3:36 PM
Wednesday, September 18, 2019 12:57 PM
All replies
-
The comma is used as carriage return:
$Example = @('one','two','three','four','Five', 'one','two','three','four','Five', 'one','two','three','four','Five', 'one','two','three','four','Five')
Or you can use a backtick character:
$Example = @('one','two','three','four','Five', ` 'one','two','three','four','Five', ` 'one','two','three','four','Five', ` 'one','two','three','four','Five')
- Marked as answer by Richard.Knight Wednesday, September 18, 2019 12:32 PM
Wednesday, September 18, 2019 7:45 AM -
Much easier than al of that is just place all on separate lines.
$Example = 'one', 'two', 'three', 'four', 'Five'
\_(ツ)_/
- Marked as answer by Richard.Knight Wednesday, September 18, 2019 12:33 PM
Wednesday, September 18, 2019 7:52 AM -
This also elim9inates the need to manage quotes and commas:
$Example = @' one two three four five '@ -split "`n" $Example
\_(ツ)_/
- Edited by jrv Wednesday, September 18, 2019 7:55 AM
- Marked as answer by Richard.Knight Wednesday, September 18, 2019 12:33 PM
Wednesday, September 18, 2019 7:54 AM -
Thank you!
Richard Knight | Collection Refresh Manager | Automate detection rules for patch \ msp files | Twitter
Wednesday, September 18, 2019 12:32 PM -
Even better, thanks jrv
Richard Knight | Collection Refresh Manager | Automate detection rules for patch \ msp files | Twitter
Wednesday, September 18, 2019 12:33 PM -
The @() isn't necessary. The comma operator makes the array. I also find using echo less tedious. I don't have to quote everything.
$Example = echo one two three four Five one two three four Five one,
two three four Five one two three four Five
One element array example:
$a = ,1
- Marked as answer by Richard.Knight Thursday, September 19, 2019 3:02 PM
- Edited by JS2010 Thursday, September 19, 2019 3:36 PM
Wednesday, September 18, 2019 12:57 PM -
I miss Perl's 'q' and 'qq' operators.
$four = 4
$x = q(one two three $four); # no interpolation of $four
$x = qq(one two three $four); # with interpolation of $four
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Wednesday, September 18, 2019 2:45 PM -
function ql { $args }
$x = ql one two three $four # with interpolation of $four- Edited by JS2010 Wednesday, September 18, 2019 3:24 PM
Wednesday, September 18, 2019 3:23 PM -
Well, not quite the same.
q(one, two? three}) would return a list with the comma, question mark and curly brace just as they appear between the parentheses. Powershell balks at the "}" and insists that you escape characters that are part of the language. In addition, using q or qq, if parentheses were part of data you can choose to use another delimiter to surround the arguments.
As for not interpolating variables, Powershell would insist on your escaping all the "$" characters. Or using the HERE doc with single quotes and using the split/join method.
I won't continue this thread, though. It's off-topic and has nothong to do with Powershell. My original reply was just a little jab at Powershell. :-)
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Thursday, September 19, 2019 2:33 AM -
very cool guys, thanks!
Richard Knight | Collection Refresh Manager | Automate detection rules for patch \ msp files | Twitter
Thursday, September 19, 2019 3:01 PM