Quick and Easy Password Generator

This is a quick and easy password/random character generator. It returns random numbers between 33 and 126 and converts the number to the corresponding ASCII character.

$password = [string]::Empty
1..32 | ForEach-Object {
    $password += [char]$(33..126 | Get-Random)
}
Parse Email Address

You can use this snippet to parse an email address and extract the different components of it. (Tip. It works for UPNs too)

New-Object "System.Net.Mail.MailAddress" -ArgumentList $emailAddress
Details
You can also send strings formatted with the display name. For example:

‘Microsoft Azure <[email protected]>’


Example
PS C:\> $emailAddress = '[email protected]'
>> New-Object "System.Net.Mail.MailAddress" -ArgumentList $emailAddress


DisplayName User       Host        Address
----------- ----       ----        -------
            mick.jones contoso.com [email protected]

  |  
Properly Capitalize a Title Using PowerShell

Being married to someone who majored in English has made me extra conscious of my spelling and capitalization. So, for my blog posts, I’ve written a script to help me ensure my titles are properly capitalized. It is not perfect (i.e. it doesn’t do a dictionary lookup), but it follows the basic APA guidelines. I thought I would share it, in case others would find it useful.

# Split the string into individual words
$words = $string.Split()
[System.Collections.Generic.List[PSObject]]$stringArray = @()
For($i = 0; $i -lt $words.Count; $i++){
    # Capitalize words of four or more letters, the first word, and the last word
    if($words[$i].Length -gt 3 -or $i -eq 0 -or $i -eq ($words.Count - 1)){
        # account for hyphen and capitalize words before and after
        $words[$i] = @($words[$i].Split('-') | ForEach-Object{
            $_.Substring(0,1).ToUpper() + $_.Substring(1,$_.Length-1)
        }) -join('-')
    } 
    # and the capitalized string to the array
    $stringArray.Add($words[$i])
}
# join the words back together to form your title
$stringArray -join(' ')


Example

PS C:\> $string = 'properly capitalize a title using PowerShell'
>> $words = $string.Split()
>> [System.Collections.Generic.List[PSObject]]$stringArray = @()
>> For($i = 0; $i -lt $words.Count; $i++){
>>     # Capitalize words of four or more letters, the first word, and the last word
>>     if($words[$i].Length -gt 3 -or $i -eq 0 -or $i -eq ($words.Count - 1)){
>>         # account for hyphen and capitalize words before and after
>>         $words[$i] = @($words[$i].Split('-') | ForEach-Object{
>>             $_.Substring(0,1).ToUpper() + $_.Substring(1,$_.Length-1)
>>         }) -join('-')
>>     }
>>     # and the capitalized string to the array
>>     $stringArray.Add($words[$i])
>> }
>> $stringArray -join(' ')

Properly Capitalize a Title Using PowerShell
  
Find Specific Word in String
[Regex]::Matches($string, '(?:^|\b)words(?:$|\b)')
Details
Matches only the specific word in the express. In this case words will return, but passwords will not.

Example
PS C:\> $string = 'A passwords should not contain words found in the dictionary.'
>> [Regex]::Matches($string, '(?:^|\b)words(?:$|\b)')



Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 31
Length   : 5
Value    : words


  |  
Find Date in MM/DD/YYYY Pattern in String
[Regex]::Matches($string, '(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))').Value
Details
List any and all valid dates since year 1900. MM and DD can have 1 or 2 digits

Example
PS C:\> $string = 'There have been solar eclipses on 03/07/1970, 2/26/1979, and 8/21/2017 in the United States.'
>> [Regex]::Matches($string, '(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))').Value

03/07/1970
2/26/1979
8/21/2017
  |  |  
Find US Phone Number in String
[Regex]::Matches($string, '((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}').Value
Details
List US phone numbers found in a string

Example
PS C:\> $string = 'Phone (123) 456-7890 | 123-456-7890'
>> [Regex]::Matches($string, '((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}').Value

(123) 456-7890
123-456-7890
  |  
Find Numbers in String
[Regex]::Matches($string, '-?\d*\.?\d+').Value
Details
List integers or floats that are positive or negative inside a string

Example
PS C:\> $string = 'The first 10 digits of pi are 3.1415926535.'
>> [Regex]::Matches($string, '-?\d*\.?\d+').Value

10
3.1415926535
  |  
Find IPv6 Address in String
[Regex]::Matches($string, '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))').Value
Details
Match IP v6 addresses

Example
PS C:\> $string = 'Pinging 2001:4860:4860::8888 with 32 bytes of data'
>> [Regex]::Matches($string, '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))').Value

2001:4860:4860::
  |  |  
Find IPv4 Address in String
[Regex]::Matches($string, '(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)').Value
Details
Match IP v4 addresses

Example
PS C:\> $string = 'Reply from 8.8.8.8: bytes=32 time=11ms TTL=54'
>> [Regex]::Matches($string, '(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)').Value

8.8.8.8
  |  |  
Find URL in String
[Regex]::Matches($string, '((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-](.*?)(?=\s))').Value
Details
Match URL with optional protocol

Example
PS C:\> $string = 'You can find tons more PowerShell examples at https://www.dowst.dev/category/strings/regex/ with new stuff uploaded every day'
>> [Regex]::Matches($string, '((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-](.*?)(?=\s))').Value

https://www.dowst.dev/category/strings/regex/
  |