PowerShell – Slicing

I was skimming through the first part of the Simple-Talk series on PowerShell Desired State Configuration, and I got to the bit that says “To install them, all you need to do is copy and paste them into your PowerShell modules folder”, and shows you the output of the author’s PSModulePath.

Get-ChildItem -Path Env:\PSModulePath | select value | fl *

poshslice1

Yeah, now compare that with the mess on my PC:

poshslice2

What’s that 4336724? Short attention span you guys have… See previous post.

Not very readable. At least, I can’t read it easily, so I need to split that into something a bit friendlier, like an actual list, one line per item. Fortunately, PowerShell has a split command that should do the job, as per this post ScriptingGuy’s Split Method in PowerShell.

Let’s give it a go:

poshslice3

As you can see from the above attempt, the result from that Get-ChildItem line is not a string, therefore the split() method is not available to us.

What it is, though, is a DictionaryEntry. And we can convert that to a string:

$([Convert]::ToString((get-childitem -Path env:\PSModulePath | select-object Value)))

poshslice4

which we can then split

$([Convert]::ToString((get-childitem -Path env:\PSModulePath | select-object Value))).split(";")

poshslice5

Which seems to work, except the results show the artefacts left behind by the string having been converted from a dictionary entry, ie the “@{Value=” at the beginning, and the “}” at the end.

So what we need to do instead is to treat it right. Treat it like the right sort of object in the first place, and iterate through the members of the dictionary collection, and slice the string properly:

(get-childitem -path Env:\PSModulePath | select-object Value) | foreach-object { $_.("Value").split(";") }

poshslice6

That’s what I was looking for. A nice, clear list that I can read easily. And in reading, I can spot some inconsistencies, such as some lines ending with “\”, and some without. And one set of modules having a “\\” in the path. Tsk.

Advertisements
This entry was posted in SQLServerPedia Syndication and tagged . Bookmark the permalink.

4 Responses to PowerShell – Slicing

  1. Or Just
    ($env:psmodulepath).Split(“;”)

  2. Pingback: Powershell Text Splitting – Curated SQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s