Split strings

  • Context

I worked recently on some Active Directory properties and needed to split a path.

$h = (
Get-ADUser UserName -Properties HomeDirectory
).HomeDirectory
$h = '\\servername\homepath.username$'

The HomeDirectory is a System.String and my variable $h contains something like \\servername\homepath.username$ (there’s a dollar at the end to hide the share).

  • Problem

I’d like to split on the \ character and to do so, you need to escape it with another backslash character because it’s a special character for regular expressions.

The official documentation About Split says that the delimiter is evaluated as a regular expression if I use the highlighted 3rd syntax:
The Options parameter is optional in the syntax and defaults to the value RegexMatch
The doc says:

RegexMatch: Use regular expression matching to evaluate the delimiter.

If I use the split operator like this:

$h -split '\\'

$h -split '\\' | measure

[string]::Empty -eq ($h -split '\\')[0]
[string]::Empty -eq ($h -split '\\')[1]
[string]::Empty -eq ($h -split '\\')[2]
[string]::Empty -eq ($h -split '\\')[3]

I get 4 results. The first 2 results of the split operation are an empty system.string.
This is expected.

I want to capture the server name and the share name, not the backslashes and not the empty strings.

  • Solution(s)

My first solution followed the idea “just ignore and discard empty strings”:

$server=$share=$null
# capture
$null,$null,$server,$share=$h -split '\\'
# display result
$server
$share

My second solution is based on the idea that “there’s probably a better way to skip these empty strings”:

$server=$share=$null
$server,$share=$h.Split('\\',2,'RemoveEmptyEntries')
# display result
$server
$share

If you’ve another great idea and want to share another (better?) way to skin the cat, please post it in the comments 🙂 .

8 thoughts on “Split strings

  1. Hi,
    perhaps there is another way.
    $H = Get-ADUser UserName -Properties HomeDirectory ==> Identify HomeDirectory Sure, it’s just a [systemString], but this corresponds to a real path. Allright ? Then, I’ll try to use Split-Path.

    Split-Path $H ==> return the parent
    Split-Path $H -leaf ==> return the Name you’re looking for.

    did I understand your pb well ?

    • Hi _Enim_
      My bad, its an UNC path but also the root folder. It seems it’s not be possible like this. But, i’m searching on the Internet and i’ve found some methods.
      First Method :
      $fullpath = “\\server\folder”
      $parentpath = [string]::join(“\”,$fullpath.Split(“\”)[2])
      $parentpath
      server
      $childrenpath = [string]::join(“\”,$fullpath.Split(“\”)[3])
      $childrenpath
      folder
      It seems that it’s exactly you’re looking for.
      Inspired by : https://stackoverflow.com/questions/18364710/split-path-with-root-unc-directory

      Second Method :
      $URI = New-Object System.Uri(“\\server\folder”)
      $uri
      $URI.Host
      server
      $uri.AbsolutePath.Split(“/”)
      folder

      If this can help you, i haven’t waste my time. Happy to be helpful
      Olivier

  2. So looking at the structure: \\something\somemore$
    I’d do:
    $h=$h.substring(2)
    $h=$h.substring(0,$h.length-1)
    PS C:\WINDOWS\system32> $h -split ‘\\’
    servername
    homepath.username

    That the things you want?

      • Hi again _Enim_

        I was sure that there was more simple. I’ve testedand tested again, and found the ultimate solution : Use the method Split, the separator will be ‘\’ and after select only the good line
        $h = “\\servername\homepath.username$”
        $Parent = $h.Split(‘\’)[2] ==> return servername
        $Children = $h.Split(‘\’)[3] ==> return homepath.username$
        [0] for the first line, [1] for the second, …
        [-1] should be for the last line (could be useful to keep in mind)

        Olivier

  3. Pingback: Dew Drop – October 4, 2019 (#3044) | Morning Dew

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.