Alex Goris IT Solutions

PowerShell: Replacing characters

At customers I sometimes see, that based on an export of SQL data from a HR system table columns can be filled with many different characters (/.&., and so on). Where the value makes sense and applies in one system might not apply to another system that is using that data.

When it comes to Active Directory for example when creating Active Directory users or groups several characters are not allowed to be used. In case of using the HR data to automatically create Active Directory users or groups based on these HR data this can be a problem.

Being at a customer, I needed to replace some characters to get it for a for the customer acceptable format to be used for creating corresponding Active Directory groups, I created the small script below. The output of this script was used as input for a Create Active Directory Group RES ONE Automation that could then successfully create that AD Group.

Note: Use the order that is needed for your own case as changing of the lines can cause different results!

Name: Alex Goris
#Version: 0.1
#Purpose: Script to replace multiple characters

$ADGroup = “Help & Supp. London, /Level 3” #Set variable with exported HR data field
$ADGroup = $ADGroup -replace “\., “, “_” #Remember that to replace a ‘.’ with another character it has to be starting with a ‘\’ character!
$ADGroup = $ADGroup -replace “\.”, “_” #Remember that to replace a ‘.’ with another character it has to be starting with a ‘\’ character!
$ADGroup = $ADGroup -replace “, “, “_” #Replace characters
$ADGroup = $ADGroup -replace “,”, “” #Replace characters
$ADGroup = $ADGroup -replace ” & “, “_” #Replace characters
$ADGroup = $ADGroup -replace “&”, “_” #Replace characters
$ADGroup = $ADGroup -replace ” “, “_” #Replace characters
$ADGroup = $ADGroup -replace “/”, “_” #Replace characters
$ADGroup = $ADGroup -replace “__”, “_” #Replace characters

The result will be when having an AD group “Help $ Supp. London, /Level 3” :  “Help_Supp_London_Level_3″.

Share on twitter
Share on linkedin
Share on email
Share on print
Share on whatsapp