Replacing a backslash character with a dot character in string

I’m working on creating a simple script that converts folder names and paths to AD security group names and I’m bumping into a slight problem.

Basically I want to strip special characters from the folder paths and that works fine, however the one thing I’m not getting is converting backslashes ‘’ to dots ‘.’

The function below does everything I want, but the last part.

function Convert-SpecialCharacter {
  param (
    [parameter(mandatory=$true)]
    [string]$Directory
  )
  
$substitutions = @{
  "'" = "";
  ' ' = '';
  '-' = '';
  '/' = '';
  ',' = '';
  '&' = '';
  'é' = 'e';
  '\.' = '';
  '\s' = '';
}
  foreach ($s in $substitutions.Keys) {
    $Directory = $Directory -replace $s, $substitutions[$s]
    $Directory = $Directory -replace '[\\/]','.'
  }
  return $Directory
} # end function Convert-SpecialCharacter

I initially had the line $Directory = $Directory -replace '[\\/]','.' in the $substitutions hash table as simply '\\' = '.'; but it didn’t work in there either and I thought it might be that it and the '\.' = ''; clashed even though I’d ordered the hash table so it should remove existing ‘.’ characters first and then do the backslash to dot conversion afterwards.

But even separating it out it doesn’t work as I wish. Basically any non-alphanumeric character I put in as a replacement gets ignored and just becomes

This path ‘UTB_Rektorer\Fsk omr. 3’ becomes ‘UTB_RektorerFskomr3’ instead of ‘UTB_Rektorer.Fskomr3’.

I’ve tried escaping the dot character in a number of ways: '\.', '[\.]' or '[.]'. I get a lot of “interesting” strings from that, but never the result I want.
I’ve also tried putting in other special characters such as ‘-’ or ‘,’ but I get the same result as with the dot-character.
Using alphanumeric characters on the other hand works just fine, using ‘aaa’ returns the string ‘UTB_RektoreraaaFskomr3’

I’ve tried googling it and I’m finding lots of results for replacing/removing a special character, but none for having it as the replacement itself.

So help me Obi-Wan Kenobi, you’re my only hope!?

Hmmm … I didn’t try the other characters but with the ones you have problems with I do not … at least if I got it right … :wink:

'UTB_Rektorer\Fsk omr. 3' -replace '\s' -replace '\\','.'

So … something like this should actually do the job …

"U/T&B_Rek'tor-er\Fs,k omr. 3" -replace "\s|-|,|&|/|\.|'" -replace '\\', '.'

I forgot the “é” thing …

"U/T&B_Rek'tor-ér\Fs,k omr. 3" -replace "\s|-|,|&|/|\.|'" -replace '\\', '.' -replace 'é', 'e'
1 Like

That was fun :slight_smile: - it worked fine in the console with a simple -replace but not as a function.

Make sure your replacement for the \ is after the replacement for the . and, importantly, make sure the hashtable is declared as [ordered]:

function Convert-SpecialCharacter {
    param (
        [parameter(mandatory = $true)]
        [string]$Directory
    )
  
    $substitutions = [ordered]@{
        "'"  = ""
        ' '  = ''
        '-'  = ''
        '/'  = ''
        ','  = ''
        '&'  = ''
        'é'  = 'e'
        '\.' = ''
        '\s' = ''
        '\\' = '.'
    }
    foreach ($s in $substitutions.Keys) {

        $Directory = $Directory -replace $s, $substitutions[$s]
    
    }
    return $Directory
} # end function Convert-SpecialCharacter

Convert-SpecialCharacter 'UTB_Rektorer\Fsk omr. 3'

Output:

UTB_Rektorer.Fskomr3
1 Like

Thank you Olaf and Matt!

Olaf’s solution was definitely more compact than my initial function and just as importantly it produced the correct result. I also learned that it was possible to add multiple -replace in one line. Without seeing that I probably would have continued creating multiple lines for more complex replace-jobs.

Matt’s solution took my original function and tweaked it into a working format. It may have been overkill for the job, when seeing the simpler -replace one-liner, but I still like it for quickly seeing what characters are being manipulated.
I will note that my initial function had the correct ordering of substitution key-value pairs, but I didn’t know about tagging it with [ordered].

I’m unfortunately unable to mark both answers as Solution, but both taught me something new.