{"id":1095,"date":"2011-02-03T09:41:28","date_gmt":"2011-02-03T14:41:28","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=1095"},"modified":"2011-02-03T09:41:28","modified_gmt":"2011-02-03T14:41:28","slug":"powershell-regular-expressions-by-the-numbers","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/","title":{"rendered":"PowerShell Regular Expressions by the Numbers"},"content":{"rendered":"<p>I've been working on something the last week that brought me back into sometimes frightening world of regular expressions. But as the saying goes, we only fear what we don't understand. So a little knowledge can be a wonderful thing. In this particular situation I was looking for a regular expression to determine if a number fell within a certain range. I know I can use an operator statement in a compound expression like ($x -gt 0) -AND ($x -le 10) but I that would require additional parsing. Plus I decided this was an opportunity to whittle away at my regular expression deficiencies. Now I know how to tell using regular expressions if a string contains a number between say 1 and 10. Here's how.<br \/>\n<!--more--><\/p>\n<p>The first step is to remember that regular expressions are all about patterns. We're not looking at values.  When constructing a number pattern you have to break it down.  Let's start simply and see if the number in my text is between 0 and 9. My regular expression uses a range: [0-9].<br \/>\n[cc lang=\"PowerShell\"]<br \/>\nPS S:\\> $x=4<br \/>\nPS S:\\> $x -match \"[0-9]\"<br \/>\nTrue<br \/>\nPS S:\\> $x=3<br \/>\nPS S:\\> $x -match \"[0-9]\"<br \/>\nTrue<br \/>\nPS S:\\> $x=-1<br \/>\nPS S:\\> $x -match \"[0-9]\"<br \/>\nTrue<br \/>\nPS S:\\> $x=11<br \/>\nPS S:\\> $x -match \"[0-9]\"<br \/>\nTrue<br \/>\n[\/cc]<br \/>\nWe're starting simple. As you can see I get the results I expected until I hit numbers like -1 and 11. Those still return True but that's not what I want. That's because regular expression patterns \"float\". Take 11. My expression says look for anything that is between 0 and 9. Looking at $matches from the last comparison I can see I in fact matched on 1.<br \/>\n[cc lang=\"PowerShell\"]<br \/>\nPS S:\\> $matches<\/p>\n<p>Name                           Value<br \/>\n----                           -----<br \/>\n0                              1<br \/>\n[\/cc]<br \/>\nThe solution is to anchor my pattern.<br \/>\n[cc lang=\"PowerShell\"]<br \/>\nPS S:\\> $x -match \"^[0-9]$\"<br \/>\nFalse<br \/>\n[\/cc]<br \/>\nWith $x still 11 now I get the result I expected because my pattern says look for something that starts (^) and ends ($) with a number between 0 and 9.<br \/>\n[cc lang=\"PowerShell\"]<br \/>\nPS S:\\> $x=6<br \/>\nPS S:\\> $x -match \"^[0-9]$\"<br \/>\nTrue<br \/>\n[\/cc]<br \/>\nA 0-9 range is rarely sufficient. More than likely I need a greater range, say up to 25. This is where it gets interesting. You need to create a pattern for each digit. We'll focus on validating 20-25. I need a pattern for the first part, a 2 and something for second part to cover 0-5. I'll end up with a more complex regular expression: \"^[2][0-5]$\".  Here's a quick way to test.<br \/>\n[cc lang=\"PowerShell\"]<br \/>\nPS S:\\> (19..30) -match \"^[2][0-5]$\"<br \/>\n20<br \/>\n21<br \/>\n22<br \/>\n23<br \/>\n24<br \/>\n25<br \/>\n[\/cc]<br \/>\nI can see that only numbers 20-25 are returned. To validate numbers 1 to 25 I need to combine my expressions.<br \/>\n[cc lang=\"PowerShell\"]<br \/>\nPS S:\\> (1..30) -match \"^([1-9]|[1][0-9]|[2][0-5])$\" | measure-object<\/p>\n<p>Count    : 25<br \/>\nAverage  :<br \/>\nSum      :<br \/>\nMaximum  :<br \/>\nMinimum  :<br \/>\nProperty :<br \/>\n[\/cc]<br \/>\nRather than list out all the matching numbers which you can so, I simply measured the result so you could see I in fact got 25 matches.  My expression is slightly more complicated.  The first part [1-9] matches 1-9. The second part, [1][0-9], after the OR (|) gets all numbers that start with 1 and end with 0-9. In other words, 10-19. The last part, [2][0-5], covers numbers 20-25. Do you see it?  Ready for one more? Here's an expression that validates 1-50.<br \/>\n[cc lang=\"PowerShell\"]<br \/>\nPS S:\\> (1..100) -match \"^([1-9]|[1-4][0-9]|[5][0])$\" | measure-object<\/p>\n<p>Count    : 50<br \/>\nAverage  :<br \/>\nSum      :<br \/>\nMaximum  :<br \/>\nMinimum  :<br \/>\nProperty :<br \/>\n[\/cc]<br \/>\nThe second part matches all numbers that start with 1-4 and end with 0-9 which covers 10-49. The last part finds numbers that start with 5 and end in 0. Taking this a step further I can now beginning writing PowerShell expressions like these:<br \/>\n[cc lang=\"PowerShell\"]<br \/>\nPS S:\\> \"File45\" -match \"^file([1-9]|[1-4][0-9]|[5][0])$\"<br \/>\nTrue<br \/>\nPS S:\\> \"File95\" -match \"^file([1-9]|[1-4][0-9]|[5][0])$\"<br \/>\nFalse<br \/>\nPS S:\\> \"File5\" -match \"^file([1-9]|[1-4][0-9]|[5][0])$\"<br \/>\nTrue<br \/>\nPS S:\\> \"File15\" -match \"^file([1-9]|[1-4][0-9]|[5][0])$\"<br \/>\nTrue<br \/>\n[\/cc]<br \/>\nTo write a pattern for a 3 digit number take what I've shown you here and simply extend it for the third digit. I'll leave that exercise to you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on something the last week that brought me back into sometimes frightening world of regular expressions. But as the saying goes, we only fear what we don&#8217;t understand. So a little knowledge can be a wonderful thing. In this particular situation I was looking for a regular expression to determine if a&#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":[534,250],"class_list":["post-1095","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-powershell","tag-regular-expressions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PowerShell Regular Expressions by the Numbers &#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\/1095\/powershell-regular-expressions-by-the-numbers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Regular Expressions by the Numbers &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"I&#039;ve been working on something the last week that brought me back into sometimes frightening world of regular expressions. But as the saying goes, we only fear what we don&#039;t understand. So a little knowledge can be a wonderful thing. In this particular situation I was looking for a regular expression to determine if a...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2011-02-03T14:41:28+00:00\" \/>\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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"PowerShell Regular Expressions by the Numbers\",\"datePublished\":\"2011-02-03T14:41:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/\"},\"wordCount\":682,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"keywords\":[\"PowerShell\",\"Regular Expressions\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/\",\"name\":\"PowerShell Regular Expressions by the Numbers &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"datePublished\":\"2011-02-03T14:41:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/1095\\\/powershell-regular-expressions-by-the-numbers\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Regular Expressions by the Numbers\"}]},{\"@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 Regular Expressions by the Numbers &#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\/1095\/powershell-regular-expressions-by-the-numbers\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Regular Expressions by the Numbers &#8226; The Lonely Administrator","og_description":"I've been working on something the last week that brought me back into sometimes frightening world of regular expressions. But as the saying goes, we only fear what we don't understand. So a little knowledge can be a wonderful thing. In this particular situation I was looking for a regular expression to determine if a...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/","og_site_name":"The Lonely Administrator","article_published_time":"2011-02-03T14:41:28+00:00","author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"PowerShell Regular Expressions by the Numbers","datePublished":"2011-02-03T14:41:28+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/"},"wordCount":682,"commentCount":2,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"keywords":["PowerShell","Regular Expressions"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/","name":"PowerShell Regular Expressions by the Numbers &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"datePublished":"2011-02-03T14:41:28+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1095\/powershell-regular-expressions-by-the-numbers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"PowerShell Regular Expressions by the Numbers"}]},{"@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":7222,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7222\/learn-more-about-powershell-and-regular-expressions\/","url_meta":{"origin":1095,"position":0},"title":"Learn More about PowerShell and Regular Expressions","author":"Jeffery Hicks","date":"February 5, 2020","format":false,"excerpt":"For many Windows-oriented IT Pros, and I used to be one of them, regular expressions was an arcane topic that was too hard to learn. And we never really had a compelling need to learn because we were busy clicky-clicking everything. Then came PowerShell and we discovered, or maybe rediscovered,\u2026","rel":"","context":"In &quot;Pluralsight&quot;","block_context":{"text":"Pluralsight","link":"https:\/\/jdhitsolutions.com\/blog\/category\/pluralsight\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/02\/image_thumb-4.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/02\/image_thumb-4.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/02\/image_thumb-4.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/02\/image_thumb-4.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":6791,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/6791\/capturing-names-with-powershell-and-regular-expressions\/","url_meta":{"origin":1095,"position":1},"title":"Capturing Names with PowerShell and Regular Expressions","author":"Jeffery Hicks","date":"July 3, 2019","format":false,"excerpt":"As you continue to learn and embrace PowerShell, you will eventually meet regular expressions. Hopefully many of you already have some fundamental knowledge. if not, the first place to start is by reading the help topic, about_regular_expressions In this article, I'm gong to introduce you to an advanced regular expression\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-9_thumb.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-9_thumb.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2019\/07\/regex-match-9_thumb.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2211,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2211\/powershell-scripting-with-validatepattern\/","url_meta":{"origin":1095,"position":2},"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":1242,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1242\/convert-transcript-to-script\/","url_meta":{"origin":1095,"position":3},"title":"Convert Transcript to Script","author":"Jeffery Hicks","date":"March 21, 2011","format":false,"excerpt":"During my PowerShell scripting best practices at Techmentor last week I mentioned a function I had to convert a PowerShell transcript to a script file. Since there's very little difference between an interactive session and a script, parsing the transcript can yield 80% or more of a script very quickly.\u2026","rel":"","context":"In &quot;Conferences&quot;","block_context":{"text":"Conferences","link":"https:\/\/jdhitsolutions.com\/blog\/category\/conferences\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7817,"url":"https:\/\/jdhitsolutions.com\/blog\/active-directory\/7817\/distinguished-parsing-with-powershell-and-regex\/","url_meta":{"origin":1095,"position":4},"title":"Distinguished Parsing with PowerShell and Regex","author":"Jeffery Hicks","date":"October 29, 2020","format":false,"excerpt":"The other day I'm chatting with my friend Gladys Kravitz about Active Directory and she makes an off-hand remark about parsing out organizational unit names from a distinguished name. On one hand, this is a pretty simple task, assuming a proper distinguished name from the Active Directory cmdlets. All you\u2026","rel":"","context":"In &quot;Active Directory&quot;","block_context":{"text":"Active Directory","link":"https:\/\/jdhitsolutions.com\/blog\/category\/active-directory\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/ignore-case.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/ignore-case.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/ignore-case.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/10\/ignore-case.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3744,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3744\/friday-fun-find-file-locking-process-with-powershell\/","url_meta":{"origin":1095,"position":5},"title":"Friday Fun: Find File Locking Process with PowerShell","author":"Jeffery Hicks","date":"March 14, 2014","format":false,"excerpt":"I was asked on Twitter this morning about a way to find out what process has a lock on a given file. I'm not aware of any PowerShell cmdlet that can do that but I figured there had to be a .NET way and if I could find a code\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"handle","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/03\/handle-300x90.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1095","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=1095"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1095\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}