Choping up a string in powershell

I have a variable called $office

$Office =   (get-aduser -Identity $Name -Properties * |select physicalDeliveryOfficeName).physicalDeliveryOfficeName 

It produces results like this

01A;514;295;632;428;485;442;277;800;175

Of course it could be shorter or longer depending on how many locations the user is in.

Each location code will ALWAYS be separated by a semicolon.

Is there a way to break that up and use each code between the semicolons as a string that I could then use a foreach statement?

Thanks

February 13th, 2015 10:06am

You can split it.  Ex.
$a = "01A;514;295;632;428;485;442;277;800;175"
$LocationArray = $a.split(";")
This will give you an array.  You can then extract the codes $LocationArray[0], $LocationArray[1], etc. via loop or whatever.
  • Marked as answer by Lishron 20 hours 14 minutes ago
Free Windows Admin Tool Kit Click here and download it now
February 13th, 2015 10:13am

I had just found this article 

http://blogs.technet.com/b/heyscriptingguy/archive/2014/06/11/powertip-split-string-with-powershell.aspx

Like it was written for me but you beat me to the bunch.

Thanks so so much!

February 13th, 2015 10:22am

You can split it.  Ex.
$a = "01A;514;295;632;428;485;442;277;800;175"
$LocationArray = $a.split(";")
This will give you an array.  You can then extract the codes $LocationArray[0], $LocationArray[1], etc. via loop or whatever.
  • Marked as answer by Lishron Friday, February 13, 2015 3:17 PM
Free Windows Admin Tool Kit Click here and download it now
February 13th, 2015 6:08pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics