validate the string in powershell

Posted on 2012-08-10
Medium Priority
Last Modified: 2013-03-01
how to validate the string in powershell script ?

i need to check the string ,  does  it contains any characters like  "!@#$%^&*()=+/\|{}[]'";:<>,?

how to do this ?
Question by:Varshini
LVL 11

Accepted Solution

netballi earned 2000 total points
ID: 38281353
here is an small example $a is the string input which contains the special character
spliting the string into individual character and comparing it to all special character ( i havent used all special character but you can improvise)

$a = "netball@i"
$b = @($a -split"(\w{1})")
foreach($d in $b)
   if($d -like "`"" -Or $d -like "`!" -Or $d -like "`@" -Or $d -like "`#" -Or $d -like "`$" -Or $d -like "`%" -Or $d -like "`^" -Or $d -like "`&")
      Write-host "$d an invalid character found"

LVL 12

Expert Comment

ID: 38281749
($string  -match "^[a-zA-Z0-9]*$" ) 

Open in new window

will evaluate to to False if the string contains any character NOT specifed

($string   -match '[/,/./</>///?/;/:/''/"/\/|]') 

Open in new window

will evaluate to to True if the string contains any character specified.

Note the use of the regular expression escape character \ before each character, because many special characters has special meanings for regular expressions.   And the doubled single-quote.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question