validate the string in powershell

Posted on 2012-08-10
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

    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

    ($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

    Are your corporate email signatures appalling?

    Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

    Join & Write a Comment

    We recently had an issue where out of nowhere, end users started indicating that their logins to our terminal server were just showing a "blank screen." After checking the usual suspects -- profiles, shell=explorer.exe in the registry, userinit.exe,…
    If you migrate a Terminal Server licenses server inside the 2008 server family, you can takte advantage of the build-in migration tool. If you like to migrate an older 2003 Server (and the installed client CALs) to a 2008 R2 server for example, you …
    This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
    This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now