Author Posts

September 28, 2017 at 7:24 pm

say somebody emailed me this text:

item1
item2
item3

if I save it as a txt file and point get-content at that text file, I get an array with 3 rows.

what's the easiest way to get that same array if I just want to paste that text list directly into the ISE instead of saving it into a text file and pointing get-content at that text file?

September 28, 2017 at 7:43 pm

This worked in my tests

$x  = @'
item1
item2
item3
'@ -split "`n"

$x.length

September 28, 2017 at 7:43 pm

I would Ctrl+C & Ctrl+V haha Or...
ise C:\temp\Saved.txt Or are you looking into more with manipulating Clip.exe?
https://brianreiter.org/2010/09/03/copy-and-paste-with-clipboard-from-powershell

September 28, 2017 at 8:06 pm

thank you Richard. the @'....'@ syntax was what I was missing. I was bringing the list in as a pure "string":

$x = "item1
item2
item3"

and then splitting it. your way is much cleaner. with the added "copying and pasting ease/laziness" benefit of not having to have the first and last items in the list on the same line as the quotation marks.

September 28, 2017 at 8:46 pm

Ah I get it. "Here String" is what you are looking for.

October 3, 2017 at 10:32 pm

i'm back. if I paste Richard's whole script into the ISE, and then add this to the bottom

$x[0].Length

I get 5. which is correct.

if I type item1, item2, item3 into the here-string, I also get 5.

but if I copy and paste

item1
item2
item3

from anywhere on this page into my here-string, the length turns into 6. even if I paste it into notepad first and then copy it from notepad into the ISE.

October 3, 2017 at 11:27 pm

Hi John,
You never really asked a question, but I assume the question is why you are getting 6 instead of 5.

It is mostly likely that you are not accounting for the carriage return. Try doing the following instead to account for the carriage return as well as the line feed.

$x  = @'
item1
item2
item3
'@ -split "`r`n"

$x.length
$x[0].length

October 4, 2017 at 4:20 pm

Curtis, I just pasted your whole script into ise, and $x.length came back as 1, and $x[0].length came back as 17.

if I use "`r|`n" as the split, then $x.length is correctly 3 and $x[0].length is correctly 5.

but again, if I paste

item1
item2
item3

into the here-string from notepad, $x.length becomes 5. so there are two extra elements in the array, which I can also see if I do $x | out-gridview (there are blank lines)

October 6, 2017 at 3:54 am

Hey John,
I was able to reproduce similar results, but not all the scenarios are the same for me as what you have represented.

In any case, you can see what is going on by doing something like this

$x  = @'
item1
item2
item3
'@ -split "`n"

Write-host "Array length: " -ForegroundColor Cyan -NoNewline
$x.length

Write-host "Element 0 length: " -ForegroundColor Cyan -NoNewline
$x[0].length

Write-host "Element 0 Character list and integer value:" -ForegroundColor Cyan
$x[0].tochararray() | % {"$_ : $([int][char]$_)"}

Results:

Array length: 3
Element 0 length: 6
Element 0 Character list and integer value:
i : 105
t : 116
e : 101
m : 109
1 : 49
 : 13

Notice in my case at the end of the string there is an ASCII character 13 "Carriage Return"
http://www.asciitable.com/

You can use this technique to determine what that sixth character is you can't see.

October 10, 2017 at 11:10 pm

Curtis, you went back to only splitting it by the newline. not by newline and carriage return.

in any event, if I paste your new script from this site into my 5.1 ise, I get an array with a length of 3 and element 0 has a length of 5. no extra characters.

interestingly, if I copy out the three members of your here-string from my ISE and paste them into notepad, they appear on one line with no spaces:

item1item2item3

but if I then copy that out of notepad, and paste it in this thread, or into another ise window, they are stacked with carriage returns (or new lines, whichever it is).
item1
item2
item3

if I type my own into notepad:

item1
item2
item3

and then paste that into the here-string in your script in my ISE, I get the same results you got. array length 3, element 0 length 6 with an extra character 13 on the end.

good times! 🙂