Powershell script to extract a substring from a string RRS feed

  • Question

  • Hi ,

    I have to extract "Location - name" from the given Distinguised name example

    CN=01HW5abcde,OU=Laptops,OU=Computers,OU=Location - Name,OU=ABC,OU=ABC - Testing,DC=XYZ,DC=Contoso,DC=com

    I tried using the following command but not able to achieve the desired output

    Code :
    $html = "CN=01HW5abcde,OU=Laptops,OU=Computers,OU=Location - Name,OU=ABC,OU=ABC - Testing,DC=XYZ,DC=Contoso,DC=com"


    ,OU=Computers,OU=Location - Name,OU=ABC

    Thanks in advannce

    Friday, February 21, 2014 8:15 AM


All replies

  • What is the goal/purpose? There are better ways of extracting pieces of a distinguished name than string manipulation.


    Friday, February 21, 2014 8:18 AM
  • It appears that you have asked a very similar question before...

    I would recommend the Get-ADPathname.ps1 script from this article, as posted previously:

    Windows IT Pro: Use PowerShell to Handle Active Directory Paths


    Friday, February 21, 2014 8:35 AM
  • Like Bill said, it really matters about what your trying to do.

    This code:  $Data= ($html.split("=")[4]).Split(",")[0] would pull out the string you have bolded.

    This would only work as long as the Location-Name string is the 4th element in the DN.

    Friday, February 21, 2014 8:37 AM
  • Get 6th element from the end of the path, name only, using Get-ADPathname script:

    PS C:\> $pathElements = Get-ADPathname `
      -Path 'CN=01HW5abcde,OU=Laptops,OU=Computers,OU=Location - Name,OU=ABC,OU=ABC - Testing,DC=XYZ,DC=Contoso,DC=com' `
      -Split -ValuesOnly
    PS C:\> $pathElements[$pathElements.Count - 6]
    Location - Name


    Friday, February 21, 2014 8:46 AM
  • Another simple way to convert strings of this form.

    PS C:\scripts> $x='CN=01HW5abcde,OU=Laptops,OU=Computers,OU=Location - Name,OU=ABC,OU=ABC - Testing,DC=XYZ,DC=Contoso,DC=com'
    PS C:\scripts> $x.Split(',')|ConvertFrom-StringData
    Name                           Value
    ----                           -----
    CN                             01HW5abcde
    OU                             Laptops
    OU                             Computers
    OU                             Location - Name
    OU                             ABC
    OU                             ABC - Testing
    DC                             XYZ
    DC                             Contoso
    DC                             com
    PS C:\scripts> ($x.Split(',')|ConvertFrom-StringData)[3]
    Name                           Value
    ----                           -----
    OU                             Location - Name


    Friday, February 21, 2014 2:09 PM
  • That technique doesn't work if object names contain commas. (I have that exact situation at work.)


    Friday, February 21, 2014 2:16 PM
  • That technique doesn't work if object names contain commas. (I have that exact situation at work.)


    Yes - that is one reason I never allow commas in names.  Even though it is possible it causes no end of issues going forwards. 

    Most domains do not have commas. Some old ones from W2K  may have started that way.


    Friday, February 21, 2014 2:43 PM
  • At work, we use an AD naming standard that requires commas. Commas are permitted in names, and we use them.

    IMO, we should use code that works no matter what characters are in use. This is the advantage of the Pathname object, of course. We don't have to say "your domain shouldn't use commas in names" and have code that depends on this. If we use the Pathname object, we don't have to worry about this.


    Friday, February 21, 2014 10:42 PM
  • That is OK. I see no issues only that using punctuation in object names crates many, many issues elsewhere and, until recently, there were not good tools.

    The quick method is usefule where we have no tools installed when the system permits.


    Friday, February 21, 2014 10:53 PM
  • The Pathname object, which is a COM object implementation of the IADsPathname interface, has been around since Windows 2000. It's not very well known, but it's really the right tool to use for parsing AD paths. It lacks a typelib so it's a bit awkward to use from PowerShell; hence the Get-ADPathname.ps1 script.


    Friday, February 21, 2014 11:01 PM