SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

PowerShell-Input-Validation Scripts

Input values are validated using various mechanisms.  let’s start by examining how you would validate input parameters in Windows PowerShell 2.0

The Inputs are validated for the below mentioned functionality

  1. PostalCode
  2. PhoneNumber
  3. EmailID
  4. IP 
  5. FileName

Download the scripts from the below link


To validate PostalCode – 99999

Function Validate-PostalCode 
 Param([Parameter(Mandatory=$true,HelpMessage="Enter a valid Postal Code xxx-xxx-xxxx")][ValidatePattern("[0-9][0-9][0-9][0-9]")]$PostalCode) 
 Write-host "The Pin Code $PostalCode is valid" 

To Validate phone number –  999-999-9999

Function validate-PhoneNumber 
  Write-host "The phone number $phoneNumber is valid" 
validate-PhoneNumber -Phonenumber 999-999-9999
 To Validate email address
function Validate-Email ([string]$Email) 
  return $Email -match "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$" 
 To Validate IP Range – 
function Validate-IPAddress ([string]$IP) 
 if($IP -match "(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})" -and -not ([int[]]$matches[1..4] -gt 255)) 
   Write-host "The $IP IP is valid" 
 To Validate the filename for ########_???_?.jpg 
function Validate-Filename 
Write-host "The filename $filename is valid" 
You can also refer the below link for more information

PowerSQL By Prashanth Jayaram

Technology enthusiast with 7+ years of experience in Database Technology. I am Microsoft Certified Professional with certificates of OCA, MCP, MCTS, MCITP developer, MCITP administration and backed with a degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication, PowerShell and Performance Tuning. Hobbies are Drawing, playing soccer and listening to Melodies songs.


Leave a comment on the original post [sqlpowershell.wordpress.com, opens in a new window]

Loading comments...