{"id":423,"date":"2012-08-14T21:20:00","date_gmt":"2012-08-14T19:20:00","guid":{"rendered":"http:\/\/henrik.familiendamgaard.dk\/blog\/?p=423"},"modified":"2012-08-15T17:39:36","modified_gmt":"2012-08-15T15:39:36","slug":"easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell","status":"publish","type":"post","link":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/","title":{"rendered":"Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell"},"content":{"rendered":"<p>Windows PowerShell introduced parameter validation which\u00a0drastically\u00a0reduces\u00a0lines of validation code &#8211; and replaces it with a couple of markup tags:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ValidateCount<\/strong>\u00a0(int minLength, int maxlength)<br \/>\nHow many arguments are allowed for a parameter.<\/p>\n<pre escaped=\"true\" lang=\"powershell\">[ValidateCount(1,3)]\r\n[String[]]\r\n$Collection<\/pre>\n<p><strong>ValidateLength<\/strong>\u00a0(int minLength, int maxlength)<\/p>\n<p>Length (in characters) of a parameter argument.<\/p>\n<pre escaped=\"true\" lang=\"powershell\">[ValidateLength(1,8)]\r\n[String[]]\r\n$Phone<\/pre>\n<p><strong>ValidatePattern\u00a0<\/strong>(string regexString, Named Parameters)<br \/>\nValid patterns for a parameter argument.<\/p>\n<pre escaped=\"true\" lang=\"powershell\">[ValidatePattern(\"[0-9][0-9][0-9][0-9]\")]\r\n[String[]]\r\n$Pin<\/pre>\n<p><strong>ValidateRange\u00a0<\/strong>(object minRange, object maxRange)<br \/>\nValid range for a parameter argument.<\/p>\n<pre escaped=\"true\" lang=\"powershell\">[ValidateRange(18,120)]\r\n[Int]\r\n$Number<\/pre>\n<p><strong>ValidateSet\u00a0<\/strong>(string[] validValues, Named Parameters)<strong><br \/>\n<\/strong>Possible values for a parameter argument.<\/p>\n<pre escaped=\"true\" lang=\"powershell\">[ValidateSet(\"One\",\"Two\",\"Three\",\"Four\")]\r\n[String]\r\n$TextString<\/pre>\n<p><strong>ValidateScript\u00a0<\/strong>(scriptblock validValues)<br \/>\nCustom script for validation<\/p>\n<pre escaped=\"true\" lang=\"powershell\">[ValidateScript({Test-Path $_ -PathType 'Container'})]\r\n[String]\r\n$Path<\/pre>\n<p><strong>ValidateNotNull<\/strong> ()<br \/>\nInput is not null<\/p>\n<pre escaped=\"true\" lang=\"powershell\">[ValidateNotNull()]\r\n[String]\r\n$Id<\/pre>\n<p><strong>ValidateNotNullOrEmpty<\/strong> ()<br \/>\nInput is neither null or empty<\/p>\n<pre escaped=\"true\" lang=\"powershell\">[ValidateNotNullOrEmpty()]\r\n[String[]]\r\n$UserName<\/pre>\n<p>&nbsp;<\/p>\n<p>Read more about validating parameters on MSDN:<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms714435(v=vs.85).aspx\">ValidateCount<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms714452(v=vs.85).aspx\">ValidateLength<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms714454(v=vs.85).aspx\">ValidatePattern<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms714421(v=vs.85).aspx\">ValidateRange<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms714434(v=vs.85).aspx\">ValidateSet<\/a><\/li>\n<\/ul>\n<p>Or use PowerShell built-in help:<\/p>\n<ul>\n<li><a title=\"about_Functions_Advanced_Parameters\" href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd347600.aspx\"><span style=\"line-height: 18px;\">Get-Help\u00a0about_Functions_Advanced_Parameters<\/span><\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<!-- AddThis Share Buttons generic via filter on the_content -->\n<!-- AddThis Related Posts generic via filter on the_content -->\n","protected":false},"excerpt":{"rendered":"<p>Windows PowerShell introduced parameter validation which\u00a0drastically\u00a0reduces\u00a0lines of validation code &#8211; and replaces it with a couple of markup tags: &nbsp; ValidateCount\u00a0(int minLength, int maxlength) How many arguments are allowed for a parameter. [ValidateCount(1,3)] [String[]] $Collection ValidateLength\u00a0(int minLength, int maxlength) Length (in characters) of a parameter argument. [ValidateLength(1,8)] [String[]] $Phone ValidatePattern\u00a0(string regexString, Named Parameters) Valid patterns &hellip; <a href=\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell<\/span><\/a><!-- AddThis Share Buttons generic via filter on wp_trim_excerpt --><br \/>\n<!-- AddThis Related Posts generic via filter on wp_trim_excerpt --><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[32],"tags":[60,179,129,130,131,132,134,133],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.6.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell \u00bb PowerShell \u00bb SYN, SYN-ACK, ACK<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Henrik Damgaard\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/\",\"url\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/\",\"name\":\"Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell \u00bb PowerShell \u00bb SYN, SYN-ACK, ACK\",\"isPartOf\":{\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#website\"},\"datePublished\":\"2012-08-14T19:20:00+00:00\",\"dateModified\":\"2012-08-15T15:39:36+00:00\",\"author\":{\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/df8ef26ea249218b5e03146bc0b33945\"},\"breadcrumb\":{\"@id\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#website\",\"url\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/\",\"name\":\"SYN, SYN-ACK, ACK\",\"description\":\"Henrik Damgaard\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/df8ef26ea249218b5e03146bc0b33945\",\"name\":\"Henrik Damgaard\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-content\/uploads\/2014\/04\/henrik.familiendamgaard.dk-henrik.jpg\",\"contentUrl\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-content\/uploads\/2014\/04\/henrik.familiendamgaard.dk-henrik.jpg\",\"caption\":\"Henrik Damgaard\"},\"description\":\"Hi I\u2019m Henrik and I write this blog based on useful topics I find interesting \u2013 mostly covering some of my technical interests : System Center Operations Manager (SCOM), BizTalk, Windows Server, PowerShell, SQL server, Internet Information Services, PHP and .NET I am currently holding a position as System Administrator with focus on Microsoft server products.\",\"sameAs\":[\"https:\/\/www.google.com\/accounts\/o8\/id?id=AItOawm9nhUQo8VH_qbGDFWjC6z6lwgwvf8pZWQ\",\"http:\/\/www.facebook.com\/nrjizer\",\"http:\/\/linkedin.com\/in\/henrikdamgaard\",\"https:\/\/twitter.com\/http:\/\/twitter.com\/nrjizer\"],\"url\":\"http:\/\/henrik.familiendamgaard.dk\/blog\/author\/henrik\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell \u00bb PowerShell \u00bb SYN, SYN-ACK, ACK","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/","twitter_misc":{"Written by":"Henrik Damgaard","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/","url":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/","name":"Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell \u00bb PowerShell \u00bb SYN, SYN-ACK, ACK","isPartOf":{"@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#website"},"datePublished":"2012-08-14T19:20:00+00:00","dateModified":"2012-08-15T15:39:36+00:00","author":{"@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/df8ef26ea249218b5e03146bc0b33945"},"breadcrumb":{"@id":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/henrik.familiendamgaard.dk\/blog\/2012\/08\/14\/easy-validating-parameter-input-using-validatecount-validatelength-validatepattern-validaterange-validateset-and-validatescript-in-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#website","url":"https:\/\/henrik.familiendamgaard.dk\/blog\/","name":"SYN, SYN-ACK, ACK","description":"Henrik Damgaard","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/henrik.familiendamgaard.dk\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/df8ef26ea249218b5e03146bc0b33945","name":"Henrik Damgaard","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/henrik.familiendamgaard.dk\/blog\/#\/schema\/person\/image\/","url":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-content\/uploads\/2014\/04\/henrik.familiendamgaard.dk-henrik.jpg","contentUrl":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-content\/uploads\/2014\/04\/henrik.familiendamgaard.dk-henrik.jpg","caption":"Henrik Damgaard"},"description":"Hi I\u2019m Henrik and I write this blog based on useful topics I find interesting \u2013 mostly covering some of my technical interests : System Center Operations Manager (SCOM), BizTalk, Windows Server, PowerShell, SQL server, Internet Information Services, PHP and .NET I am currently holding a position as System Administrator with focus on Microsoft server products.","sameAs":["https:\/\/www.google.com\/accounts\/o8\/id?id=AItOawm9nhUQo8VH_qbGDFWjC6z6lwgwvf8pZWQ","http:\/\/www.facebook.com\/nrjizer","http:\/\/linkedin.com\/in\/henrikdamgaard","https:\/\/twitter.com\/http:\/\/twitter.com\/nrjizer"],"url":"http:\/\/henrik.familiendamgaard.dk\/blog\/author\/henrik\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/posts\/423"}],"collection":[{"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/comments?post=423"}],"version-history":[{"count":14,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/posts\/423\/revisions"}],"predecessor-version":[{"id":426,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/posts\/423\/revisions\/426"}],"wp:attachment":[{"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/media?parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/categories?post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/henrik.familiendamgaard.dk\/blog\/wp-json\/wp\/v2\/tags?post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}