PowerShell: Replace special characters

Discovery

While browsing the interwebs I came across a neat little trick (on Martin Zugec blog):
[System.Text.RegularExpressions.Regex]::Replace($Text,"[^1-9a-zA-Z_]"," ")

Description

This cmdlet simply removes all special characters and replaces them with a space.
I created a verry basic cmdlet using this bit of code

Function Remove-SpecialCharacters {
	<#
	.SYNOPSIS
		Removes special characters from a string.

	.DESCRIPTION
		Any character appart from alphanumerical characters and underscores will be replaced by an empty character.
	
	.EXAMPLE
		Remove-SpecialCharacters "Test-String's"
		This command will return "TestStrings"

	.OUTPUTS
		System.String
	#>
	[CmdletBinding()]
	Param(
		[Parameter(
			Mandatory = $true,
			Position = 1,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true
		)]
		[Alias("Input")]
		[string] $InputString
	)
	Begin {}
	Process {
		$InputString = $InputString.Replace('á','a')
		$InputString = $InputString.Replace('Á','A')
		$InputString = $InputString.Replace('à','a')
		$InputString = $InputString.Replace('À','A')
		$InputString = $InputString.Replace('â','a')
		$InputString = $InputString.Replace('Â','A')
		$InputString = $InputString.Replace('ä','a')
		$InputString = $InputString.Replace('Ä','A')
		$InputString = $InputString.Replace('ã','a')
		$InputString = $InputString.Replace('Ã','A')
		
		$InputString = $InputString.Replace('ç','c')
		
		$InputString = $InputString.Replace('é','e')
		$InputString = $InputString.Replace('É','E')
		$InputString = $InputString.Replace('è','e')
		$InputString = $InputString.Replace('È','E')
		$InputString = $InputString.Replace('ê','e')
		$InputString = $InputString.Replace('Ê','E')
		$InputString = $InputString.Replace('ë','e')
		$InputString = $InputString.Replace('Ë','E')
		
		$InputString = $InputString.Replace('í','i')
		$InputString = $InputString.Replace('Í','I')
		$InputString = $InputString.Replace('ì','i')
		$InputString = $InputString.Replace('Ì','I')
		$InputString = $InputString.Replace('î','i')
		$InputString = $InputString.Replace('Î','I')
		$InputString = $InputString.Replace('ï','i')
		$InputString = $InputString.Replace('Ï','I')
		
		$InputString = $InputString.Replace('ñ','n')
		$InputString = $InputString.Replace('Ñ','N')
		
		$InputString = $InputString.Replace('ó','o')
		$InputString = $InputString.Replace('Ó','O')
		$InputString = $InputString.Replace('ò','o')
		$InputString = $InputString.Replace('Ò','O')
		$InputString = $InputString.Replace('ô','o')
		$InputString = $InputString.Replace('Ô','O')
		$InputString = $InputString.Replace('ö','o')
		$InputString = $InputString.Replace('Ö','O')
		$InputString = $InputString.Replace('õ','o')
		$InputString = $InputString.Replace('Õ','O')
		
		$InputString = $InputString.Replace('ú','u')
		$InputString = $InputString.Replace('Ú','U')
		$InputString = $InputString.Replace('ù','u')
		$InputString = $InputString.Replace('Ù','U')
		$InputString = $InputString.Replace('û','u')
		$InputString = $InputString.Replace('Û','U')
		$InputString = $InputString.Replace('ü','u')
		$InputString = $InputString.Replace('Ü','U')
		
		# Remove rest
		$strOutput = [System.Text.RegularExpressions.Regex]::Replace($InputString,"[^0-9a-zA-Z_]","")
		Write-Output $strOutput
	}
	End {}
}

Example

Remove-SpecialCharacters  "First-Name Last 'Name"

Result:
FirstName Last Name

I will probably use it in a user creation script sins I don’t really like all those special characters in email addresses and such.

Advertisements

Tags: , , , , , , , ,

One response to “PowerShell: Replace special characters”

  1. Artur Rodrigues says :

    You should check the System.Globalization Namespace http://blogs.msdn.com/b/michkap/archive/2007/05/14/2629747.aspx

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: