Monday, April 16, 2012

PowerShell: Split strings

Oh that's an easy one, but I always forget it after a couple of weeks. Well, I don't use it that often, but it is kind of useful. So let's split all the strings!

$URL = "http://YOURURL.TLD/site/topic
$strings = $URL.split("/")
That's how you split your strings. Now you have a couple of strings, because we split them at the "/". It may not be the smartest idea, but this way you start building the needed address the way we want it.

The result will look something like this:
$strings[0] -> http:
$strings[1] -> ~empty~
$strings[2] -> YOURURL.TLD
$strings[3] -> site
$strings[4] -> topic
But why should anybody do it that way? For example your are working with SharePoint and using the Get-SPWeb command in PowerShell, you will get the address of your web, but that may be not the address you need for a picture or a link. So now you can just edit the link you need.

In my case, I needed to do that to change the "http" to "https". I wrote a blogpost about exchanging strings, which worked fine when I had to change only links. But it wouldn't work with the pictures I implemented, which also needed an https address. And with the .split()-method I was able to recreate the address in the way I wanted it to be.

No comments:

Post a Comment