In PowerShell, you will constantly find yourself iterating through collections of data. Perhaps you have a list of users or folders to process. Often what ends up happening is you will obtain this data through some other cmdlet. Perhaps Get-ADUser or Get-ChildItem. This works well but what happens if you’re testing and you only want to iterate through a subset of this data? Or perhaps You have a CSV or log file with a bunch of text in it that you need to iterate through? There are many ways to go about this. The most common perhaps is saving the data into a file and then using the Import-CSV cmdlet. That’s fine but it annoys me as if I’m just testing, I don’t want to create more files I’m likely just going to forget about anyway. Another option is to declare an array. This is better but you have to wrap each line in quotes. If you are copying and pasting 100 rows from notepad, you have to do extra work to add these quotes. That’s no good. To solve this once and for all, I have spent a bunch of time trying to come up with the quickest and easiest possible way of adding a block of text and converting it into an array. I’d like to share it with you.
The core feature we are going to take advantage of here is something called a here string. A here string can be thought of as a block of text where PowerShell doesn’t attempt to interfere with any special characters it may find. In our case, we’re going to take advantage of the fact that here strings do not strip out the new line (aka carriage return).
Let’s say that we have a list of processes in notepad that we want to get details on. That list look like the screenshot below. Not that there is absolutely no special formatting or additional characters, just as you’d typically find this kind of data "in the wild".
What’s the fastest possible way to get the details of these processes?
-
Declare the variable that will store these processes like this:
$Processes = @"
On the subsequent lines, all you have do now is copy and paste your lines from notepad. No additional formatting or changes required!
So far so good. Now for the secret sauce. We need to terminate the here string by adding a "@ on to the last line. Since this here string contains the newline character at the end of each process, we’re now going to split it on the newline such that each process will become its own element. Lastly, because we’re often going to be copying and pasting from other sources, there are likely going to be leading or trailing spaces. We don’t want to micro manage that so let’s just remove those now as well by using the trim method. The result?
In other words, you can blindly copy and paste whatever text you want into the yellow area above and without any additional work, automatically iterate through it, line by line. Below is the syntax. I’ve even created a PowerGUI snippet for this so now any time I need to work with some random text data on the clipboard, all I have to do is press Control I and I’m ready to process my data!
$Array = @"
[insert
rows
of text here]
"@ -split "`n" | % { $_.trim() }
To really drive the point home, using this technique, you can also reference any specific line (not character!) by array element:
This is one of my favorite new tools in my tool chest so I wanted to share.
10 comments
Skip to comment form
Thank you, Robert!
This is such a great! Exactly what I’d been looking for
Thanks!
Okay, that was just awesome! Thank you for sharing!!!
I know, right? I use that technique now probably 5 times a day, every day. It really is awesome. 🙂
Thanks, this really worked but what can i do if there is any variable which contains value like this. Could you please help
I’m afraid I don’t understand your question. What do you mean by “value you like this?”
Thanks!!!
this worked like charm
Thanks – I was exactlly searching for that Powershell Stuff!
Again, Many Thanks; very useful