A regular expression (regex) describes text to match. In PowerShell automation, use one when a string must follow a rule: remove characters that cannot appear in a URL, or pull a KB identifier from longer page text. Regex is powerful but easy to misuse, so define the allowed or required pattern narrowly and verify the result. section 11.3.3 section 13.3.1
Build a pattern around the rule the automation needs:
- Use square brackets to create a character list. In a URL-cleanup pattern, the list contains permitted letters, digits, underscores, dashes, single quotes, and periods. section 11.3.3
- Place ^ inside the character list to negate it: match characters not allowed by the list, then replace each match with an empty string. section 11.3.3
- Use a literal prefix plus a digit count when an identifier has a known form. KB[0-9]{7} matches KB followed by seven digits. section 13.3.1
- Keep regex as one tool among splits, substrings, indexes, and replacements; use it where its matching rule makes the task clearer or more practical. section 1.2
[regex]::Replace($string, "[^0-9a-zA-Z_\-'\.]", "")
This replacement removes every character outside the allowed URL set. For example, my-page (name) becomes my-pagename, preserving only characters accepted by the rule. section 11.3.3
Extract KB numbers from scraped update links
After selecting update links with XPath, match each link's InnerText with KB[0-9]{7}. Store the matched value with the full title and parsed update ID in a custom PowerShell object, then test the function against multiple KB numbers. section 13.3.1


























