PowerShell: How to find Special Characters in a String

Sometimes special characters are a nuisance. If you are trying to create some user accounts in on-premise or cloud environments, you should avoid special characters in usernames. In this blog post I will show how to find this special characters.

$a contains some names with special characters.

$a = 'Jañinò','Janino','Häns','Jörg'

Then I use a regex statement to retrieve only names with special characters. I also exlclude german umlauts. Note that “Jörg” and “Häns” contain german umlauts. The result should be only ‘Jañinò’.

Foreach ($i in $a) {

If ($i -match '[^a-zA-Z]' -and $i -notmatch '[ö,ü,ä,Ä,Ö,Ü]')
Write-Warning "$i"

Nice one. Mission accomplished.

Categories: PowerShell

Tagged as: ,

2 replies »

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.