{"id":2219,"date":"2012-04-20T12:11:30","date_gmt":"2012-04-20T16:11:30","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=2219"},"modified":"2012-04-20T12:11:30","modified_gmt":"2012-04-20T16:11:30","slug":"powershell-scripting-with-validatelength","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/","title":{"rendered":"PowerShell Scripting with [ValidateLength]"},"content":{"rendered":"<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/thisbig.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/thisbig.png\" alt=\"\" title=\"thisbig\" width=\"182\" height=\"130\" class=\"alignleft size-full wp-image-2225\" \/><\/a>In continuing the exploration of parameter validation attributes, today we'll look at [ValidateLength()]. You can use this attribute in your PowerShell scripting to validate that a parameter value is at least a certain length and no more and a certain length. In other words, it has to be just right. Here's what it looks like:<\/p>\n<p><code lang=\"PowerShell\"><br \/>\n[ValidateLength(2,10)]<br \/>\n[string]$Fooby<br \/>\n<\/code><\/p>\n<p>In this example any value for -Fooby must be at least 2 characters long and no more than 10. Anything outside of that range and PowerShell will raise an exception. You have to supply both values in the validation attribute. An alternative would be to use [<a href=\"http:\/\/bit.ly\/HKVu08\" title=\"Read my article on this validation attribute\" target=\"_blank\">ValidateScript<\/a>()].<\/p>\n<p><code lang=\"PowerShell\"><br \/>\n[ValidateScript({ $_.Length -ge 5})]<br \/>\n[string]$Name<br \/>\n<\/code><\/p>\n<p>Now, any value for -Name must be at least 5 characters and there is no upper limit. Here's a more complete example.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\n#requires -version 2.0<\/p>\n<p>[cmdletbinding(SupportsShouldProcess=$True)]<\/p>\n<p>Param (<br \/>\n[Parameter(Position=0,Mandatory=$True,HelpMessage=\"Enter name between 5 and 15 characters\")]<br \/>\n[ValidateLength(5,15)]<br \/>\n[string]$Name,<br \/>\n[Parameter(Position=1,Mandatory=$True,HelpMessage=\"Enter password between 7 and 64 characters\")]<br \/>\n[ValidateLength(7,64)]<br \/>\n[ValidatePattern({^\\S+$})]<br \/>\n[string]$Password,<br \/>\n[string]$Computername=$env:computername,<br \/>\n[switch]$Passthru<br \/>\n)<\/p>\n<p>Write-Host \"Creating $name with password of $Password on $computername\" -ForegroundColor Green<br \/>\n[ADSI]$Server=\"WinNT:\/\/$computername\"<br \/>\n$User=$server.Create(\"User\",$Name)<\/p>\n<p>if ($pscmdlet.ShouldProcess($User.Path)) {<br \/>\n    Write-Host \"Committing new account changes\" -ForegroundColor Green<\/p>\n<p>    <#\n      #uncomment the next lines if you really, really want to do this\n    $User.SetInfo()    \n    Write-Host \"Setting password\" -ForegroundColor Green\n    $User.SetPassword($Password)\n    If ($passthru) {\n        Write-Output $User\n    }\n    #><br \/>\n}<br \/>\n<\/code><\/p>\n<p>This script will create a local user account. I'm asking that the user name be between 5 and 15 characters and that the password be between 7 and 64 characters. I've also added a second validation check on the password using [<a href=\"http:\/\/bit.ly\/J7WCLC\" title=\"Read my article on this validation attribute\" target=\"_blank\">ValidatePattern<\/a>()] to verify it doesn't contain any spaces.<\/p>\n<p>The exception message you see with [ValidateLength()] depends on where you fall short.<\/p>\n<p><code lang=\"DOS\"><br \/>\nPS S:\\> .\\Demo-ValidateLength.ps1 Jeff Password123<br \/>\nC:\\scripts\\Demo-ValidateLength.ps1 : Cannot validate argument on parameter 'Nam<br \/>\ne'. The number of characters (4) in the argument is too small. Specify an argum<br \/>\nent whose length is greater than or equal to \"5\" and then try the command again<br \/>\n.<br \/>\nAt line:1 char:26<br \/>\n+ .\\Demo-ValidateLength.ps1 <<<<  Jeff Password123\n    + CategoryInfo          : InvalidData: (:) [Demo-ValidateLength.ps1], Para\n   meterBindingValidationException\n    + FullyQualifiedErrorId : ParameterArgumentValidationError,Demo-ValidateLe\n   ngth.ps1\n\nPS S:\\> .\\Demo-ValidateLength.ps1 JeffJeffJeffJeff Password123<br \/>\nC:\\scripts\\Demo-ValidateLength.ps1 : Cannot validate argument on parameter 'Nam<br \/>\ne'. The argument length of 16 is too long. Shorten the length of the argument t<br \/>\no less than or equal to \"15\" and then try the command again.<br \/>\nAt line:1 char:26<br \/>\n+ .\\Demo-ValidateLength.ps1 <<<<  JeffJeffJeffJeff Password123\n    + CategoryInfo          : InvalidData: (:) [Demo-ValidateLength.ps1], Para\n   meterBindingValidationException\n    + FullyQualifiedErrorId : ParameterArgumentValidationError,Demo-ValidateLe\n   ngth.ps1\n<\/code><\/p>\n<p>If you'd like, you can download <a href='http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/Demo-ValidateLength.txt'>Demo-ValidateLength<\/a> and try it out for yourself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In continuing the exploration of parameter validation attributes, today we&#8217;ll look at [ValidateLength()]. You can use this attribute in your PowerShell scripting to validate that a parameter value is at least a certain length and no more and a certain length. In other words, it has to be just right. Here&#8217;s what it looks like:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[307,534,540,372],"class_list":["post-2219","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-parameter","tag-powershell","tag-scripting","tag-validation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PowerShell Scripting with [ValidateLength] &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Scripting with [ValidateLength] &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"In continuing the exploration of parameter validation attributes, today we&#039;ll look at [ValidateLength()]. You can use this attribute in your PowerShell scripting to validate that a parameter value is at least a certain length and no more and a certain length. In other words, it has to be just right. Here&#039;s what it looks like:...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2012-04-20T16:11:30+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/thisbig.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"PowerShell Scripting with [ValidateLength]\",\"datePublished\":\"2012-04-20T16:11:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/\"},\"wordCount\":203,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/04\\\/thisbig.png\",\"keywords\":[\"parameter\",\"PowerShell\",\"Scripting\",\"Validation\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/\",\"name\":\"PowerShell Scripting with [ValidateLength] &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/04\\\/thisbig.png\",\"datePublished\":\"2012-04-20T16:11:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/#primaryimage\",\"url\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/04\\\/thisbig.png\",\"contentUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/04\\\/thisbig.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2219\\\/powershell-scripting-with-validatelength\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Scripting with [ValidateLength]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell Scripting with [ValidateLength] &#8226; The Lonely Administrator","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":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Scripting with [ValidateLength] &#8226; The Lonely Administrator","og_description":"In continuing the exploration of parameter validation attributes, today we'll look at [ValidateLength()]. You can use this attribute in your PowerShell scripting to validate that a parameter value is at least a certain length and no more and a certain length. In other words, it has to be just right. Here's what it looks like:...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/","og_site_name":"The Lonely Administrator","article_published_time":"2012-04-20T16:11:30+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/thisbig.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"PowerShell Scripting with [ValidateLength]","datePublished":"2012-04-20T16:11:30+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/"},"wordCount":203,"commentCount":5,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/thisbig.png","keywords":["parameter","PowerShell","Scripting","Validation"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/","name":"PowerShell Scripting with [ValidateLength] &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/thisbig.png","datePublished":"2012-04-20T16:11:30+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/#primaryimage","url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/thisbig.png","contentUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/thisbig.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2219\/powershell-scripting-with-validatelength\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"PowerShell Scripting with [ValidateLength]"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2211,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2211\/powershell-scripting-with-validatepattern\/","url_meta":{"origin":2219,"position":0},"title":"PowerShell Scripting with [ValidatePattern]","author":"Jeffery Hicks","date":"April 19, 2012","format":false,"excerpt":"I've been writing about a number of parameters attributes you can include in your PowerShell scripting to validate parameter values. Today I want to cover using a regular expression pattern to validate a parameter value. I'm going to assume you have a rudimentary knowledge of how to use regular expressions\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/squarepattern-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2247,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2247\/powershell-scripting-with-validatecount\/","url_meta":{"origin":2219,"position":1},"title":"PowerShell Scripting with [ValidateCount]","author":"Jeffery Hicks","date":"April 26, 2012","format":false,"excerpt":"Here's another parameter validation attribute you might want to use in your PowerShell scripting and functions. If your parameter can take an array of values, you might want to limit that array to a certain size. For example, your parameter can take an array of computer names but you don't\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/04\/validatecount-ok-300x66.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2306,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2306\/powershell-scripting-with-validatenotnullorempty\/","url_meta":{"origin":2219,"position":2},"title":"PowerShell Scripting with [ValidateNotNullorEmpty]","author":"Jeffery Hicks","date":"May 15, 2012","format":false,"excerpt":"I've been writing about the different parameter validation attributes that you can use in your PowerShell scripting. One that I use in practically every script is [ValidateNotNullorEmpty()]. This validation will ensure that something is passed as a parameter value. I'm not talking about making a parameter mandatory; only that if\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/05\/validatenotnullorempty-300x141.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2188,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2188\/powershell-scripting-with-validaterange\/","url_meta":{"origin":2219,"position":3},"title":"PowerShell Scripting with [ValidateRange]","author":"Jeffery Hicks","date":"April 11, 2012","format":false,"excerpt":"After my post yesterday on using the ValidateScript attribute with PSCredentials, I thought you might find it helpful to have a brief discussion on some other parameter validation attributes such as [ValidateRange()]. You can use this attribute if you want to verify that a given parameter value falls between some\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2206,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2206\/powershell-scripting-with-validateset\/","url_meta":{"origin":2219,"position":4},"title":"PowerShell Scripting with [ValidateSet]","author":"Jeffery Hicks","date":"April 16, 2012","format":false,"excerpt":"Today we'll continue our exploration of the parameter validation attributes you can use in you PowerShell scripting. We've already looked at [ValidateRange] and [ValidateScript]. Another attribute you are likely to use is [ValidateSet()]. You can use this to verify that the parameter value belongs to a pre-defined set. To use,\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2193,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2193\/powershell-scripting-with-validatescript\/","url_meta":{"origin":2219,"position":5},"title":"PowerShell Scripting with [ValidateScript]","author":"Jeffery Hicks","date":"April 12, 2012","format":false,"excerpt":"The last few days we've been looking at parameter validation attributes you might use in a script of function. Yesterday I wrote about [ValidateRange] and demonstrated how you might use it. That attribute works fine for any values that can be evaluated as numbers. But dates are a different story.\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2219"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2219\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}