none
-split leaves leading and trailing whitespace RRS feed

  • Question

  • Hi,

    I'm trying to write a script which gets a list of groups, trims off some text at the start and end, then creates an OU where one doesn't exist. At the moment -split seems to leave whitespace at the start and end of the string I've extracted and I can't figure out how to trim this off - any ideas?

    $baseOUDNForOUs = ""
    $baseOUDNForGroups = ""
    $OUList = Get-ADOrganizationalUnit -Filter * -SearchBase $baseOUDNForOUs
    
    foreach ($group in Get-ADGroup -Filter "Name -like 'group-*-computers'" -SearchBase $baseOUDNForGroups) {
        $splitStart = $group.Name -split 'group-'
        $splitEnd = $splitStart -split '-computers'
    
        if ($OUList.name -match $splitEnd){
            write-host "Found" $splitEnd
        } else { 
            write-host "not found" $splitEnd
        }
    }

    I've tried substring(), trim() and replace() but split() seemed to work better, albeit I can't get rid of whitespace / space character at the start and end of the string.

    Many thanks

    Thursday, January 23, 2020 1:44 PM

Answers

  • Then perhaps use replace.

    $Name = "Group-Group1-Full-Name-Computer"
    $Group = $Name.Replace("Group-", "").Replace("-Computer", "")
    Write-Host "=$Group="

    But if your names are similar to your example, I don't know where the leading/trailing spaces could come from.

    Also, the Trim() function works for me:

    $Group = $Name.Replace("Group-", "").Replace("-Computer", "").Trim()
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Thursday, January 23, 2020 3:40 PM
    Moderator

All replies

  •  $start,$middle,$end = $group.Name -split '-'

    \_(ツ)_/


    • Edited by jrv Thursday, January 23, 2020 1:56 PM
    Thursday, January 23, 2020 1:55 PM
  • Thanks but I can't split on '-' as these will exist in the middle section, I need to split on 2 distinct strings that appear at the start and end of the group name.

    Start = 'group-'

    End = '-computers'

    Thursday, January 23, 2020 2:32 PM
  • Does this help?

    # If the name has no embedded spaces.
    $Name = "Group-Sales-Computers"
    $Group = $Name.Split("-")[1]
    Write-Host "=$Group="
    
    # If the name can have embedded spaces.
    $Name = "Group-  Sales  -Computers"
    $Group = $Name.Split("-")[1].Replace(" ", "")
    Write-Host "=$Group="
    

    The added leading and trailing "=" characters are to confirm that there are no leading or trailing spaces.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, January 23, 2020 3:11 PM
    Moderator
  • Thanks, I'll take a look. Please can we try and split the group name based on a string e.g.

    DeleteMe-Group1-Full-Name-Delete

    Strip off DeleteMe- from the start

    Strip off -Delete from the end

    Which will then leave Group1-Full-Name as the value left

    Thanks

    Thursday, January 23, 2020 3:16 PM
  • I've also tried this with trimstart and trimend but couldn't get that to work as planned either.
    Thursday, January 23, 2020 3:33 PM
  • Then perhaps use replace.

    $Name = "Group-Group1-Full-Name-Computer"
    $Group = $Name.Replace("Group-", "").Replace("-Computer", "")
    Write-Host "=$Group="

    But if your names are similar to your example, I don't know where the leading/trailing spaces could come from.

    Also, the Trim() function works for me:

    $Group = $Name.Replace("Group-", "").Replace("-Computer", "").Trim()
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Thursday, January 23, 2020 3:40 PM
    Moderator
  • Thanks - I think it's because split creates objects for each thing it splits then it outputs these as empty objects and so a space in the variable. I've been playing around with other functions and have come up with just removing a defined number of characters from the start and end i.e.

    $subStart = $group.Name.Substring(9)
    $subEnd = $subStart.Substring(0,$subStart.Length-7)

    But your replace command also works so I'll mark that as the answer, thanks!

    • Edited by David4576 Thursday, January 23, 2020 3:49 PM
    Thursday, January 23, 2020 3:46 PM
  • Thanks but I can't split on '-' as these will exist in the middle section, I need to split on 2 distinct strings that appear at the start and end of the group name.

    Start = 'group-'

    End = '-computers'

    Then the easiest ways to extract the middle is thiis:

    $_.Name -replace '^group-(.*)-computers$','$1'


    \_(ツ)_/



    • Edited by jrv Thursday, January 23, 2020 5:26 PM
    • Proposed as answer by jrv Thursday, January 23, 2020 5:26 PM
    Thursday, January 23, 2020 5:25 PM