Combine an array into a string with semicolon separator
$array = 'The','quick','brown','fox','jumps','over','the','lazy','dog.'
$array -join(';')
Details
Uses a join to combine the strings

Example
PS C:\> $array = 'The','quick','brown','fox','jumps','over','the','lazy','dog.'
>> $array -join(';')

The;quick;brown;fox;jumps;over;the;lazy;dog.
  |  |  
Combine an array into a string with space separator
$array = 'The','quick','brown','fox','jumps','over','the','lazy','dog.'
$array -join(' ')
Details
Uses a join to combine the strings

Example
PS C:\> $array = 'The','quick','brown','fox','jumps','over','the','lazy','dog.'
>> $array -join(' ')

The quick brown fox jumps over the lazy dog.
  |  |  
Combine multiple strings
$string1 = "The quick brown fox "
$string2 ="jumps over the lazy dog"
$string1 + $string2
Details
This should only be used for a fixed number of strings.
For combining a lot of strings or an unknown number, refer to the StringBuilder

Example
PS C:\> $string1 = "The quick brown fox "
>> $string2 ="jumps over the lazy dog"
>> $string1 + $string2

The quick brown fox jumps over the lazy dog
  
Composite Format String
$string = 'The quick brown {0} jumps over the lazy {1}.'
$string -f 'fox','dog'
Details
Allows for the easy replacement of certain characters. Great when dealing with multiple languages.

Example
PS C:\> $string = 'The quick brown {0} jumps over the lazy {1}.'
>> $string -f 'fox','dog'

The quick brown fox jumps over the lazy dog.
  
Convert date to string with specific format
[datetime]$date = Get-Date
$date.ToString('yyyy-MM-dd')
Details
See the section on working with dates and times for more on this topic

Example
PS C:\> [datetime]$date = Get-Date
>> $date.ToString('yyyy-MM-dd')

2019-03-28
  
Convert object output to string
Get-Alias | Out-String
Details
Convert object output to string

Example
PS C:\> Get-Alias | Out-String


CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           % -> ForEach-Object
Alias           ? -> Where-Object
Alias           ac -> Add-Content
Alias           asnp -> Add-PSSnapin
Alias           cat -> Get-Content
Alias           cd -> Set-Location
Alias           CFS -> ConvertFrom-String                          3.1.0.0    Microsoft.PowerShell.Utility
Alias           chdir -> Set-Location
Alias           clc -> Clear-Content
Alias           clear -> Clear-Host
Alias           clhy -> Clear-History
Alias           cli -> Clear-Item
Alias           clp -> Clear-ItemProperty
Alias           cls -> Clear-Host
Alias           clv -> Clear-Variable
Alias           cnsn -> Connect-PSSession
Alias           compare -> Compare-Object
Alias           copy -> Copy-Item
Alias           cp -> Copy-Item
Alias           cpi -> Copy-Item
Alias           cpp -> Copy-ItemProperty
Alias           curl -> Invoke-WebRequest
Alias           cvpa -> Convert-Path
Alias           dbp -> Disable-PSBreakpoint
Alias           del -> Remove-Item
Alias           diff -> Compare-Object
Alias           dir -> Get-ChildItem
Alias           dnsn -> Disconnect-PSSession
Alias           ebp -> Enable-PSBreakpoint
Alias           echo -> Write-Output
Alias           epal -> Export-Alias
Alias           epcsv -> Export-Csv
Alias           epsn -> Export-PSSession
Alias           erase -> Remove-Item
Alias           etsn -> Enter-PSSession
Alias           exsn -> Exit-PSSession
Alias           fc -> Format-Custom
Alias           fhx -> Format-Hex                                  3.1.0.0    Microsoft.PowerShell.Utility
Alias           fl -> Format-List
Alias           foreach -> ForEach-Object
Alias           ft -> Format-Table
Alias           fw -> Format-Wide
Alias           gal -> Get-Alias
Alias           gbp -> Get-PSBreakpoint
Alias           gc -> Get-Content
Alias           gci -> Get-ChildItem
Alias           gcm -> Get-Command
Alias           gcs -> Get-PSCallStack
Alias           gdr -> Get-PSDrive
Alias           ghy -> Get-History
Alias           gi -> Get-Item
Alias           gjb -> Get-Job
Alias           gl -> Get-Location
Alias           gm -> Get-Member
Alias           gmo -> Get-Module
Alias           gp -> Get-ItemProperty
Alias           gps -> Get-Process
Alias           gpv -> Get-ItemPropertyValue
Alias           group -> Group-Object
Alias           gsn -> Get-PSSession
Alias           gsnp -> Get-PSSnapin
Alias           gsv -> Get-Service
Alias           gu -> Get-Unique
Alias           gv -> Get-Variable
Alias           gwmi -> Get-WmiObject
Alias           h -> Get-History
Alias           history -> Get-History
Alias           icm -> Invoke-Command
Alias           iex -> Invoke-Expression
Alias           ihy -> Invoke-History
Alias           ii -> Invoke-Item
Alias           ipal -> Import-Alias
Alias           ipcsv -> Import-Csv
Alias           ipmo -> Import-Module
Alias           ipsn -> Import-PSSession
Alias           irm -> Invoke-RestMethod
Alias           ise -> powershell_ise.exe
Alias           iwmi -> Invoke-WMIMethod
Alias           iwr -> Invoke-WebRequest
Alias           kill -> Stop-Process
Alias           lp -> Out-Printer
Alias           ls -> Get-ChildItem
Alias           man -> help
Alias           md -> mkdir
Alias           measure -> Measure-Object
Alias           mi -> Move-Item
Alias           mount -> New-PSDrive
Alias           move -> Move-Item
Alias           mp -> Move-ItemProperty
Alias           mv -> Move-Item
Alias           nal -> New-Alias
Alias           ndr -> New-PSDrive
Alias           ni -> New-Item
Alias           nmo -> New-Module
Alias           npssc -> New-PSSessionConfigurationFile
Alias           nsn -> New-PSSession
Alias           nv -> New-Variable
Alias           ogv -> Out-GridView
Alias           oh -> Out-Host
Alias           popd -> Pop-Location
Alias           ps -> Get-Process
Alias           pushd -> Push-Location
Alias           pwd -> Get-Location
Alias           r -> Invoke-History
Alias           rbp -> Remove-PSBreakpoint
Alias           rcjb -> Receive-Job
Alias           rcsn -> Receive-PSSession
Alias           rd -> Remove-Item
Alias           rdr -> Remove-PSDrive
Alias           ren -> Rename-Item
Alias           ri -> Remove-Item
Alias           rjb -> Remove-Job
Alias           rm -> Remove-Item
Alias           rmdir -> Remove-Item
Alias           rmo -> Remove-Module
Alias           rni -> Rename-Item
Alias           rnp -> Rename-ItemProperty
Alias           rp -> Remove-ItemProperty
Alias           rsn -> Remove-PSSession
Alias           rsnp -> Remove-PSSnapin
Alias           rujb -> Resume-Job
Alias           rv -> Remove-Variable
Alias           rvpa -> Resolve-Path
Alias           rwmi -> Remove-WMIObject
Alias           sajb -> Start-Job
Alias           sal -> Set-Alias
Alias           saps -> Start-Process
Alias           sasv -> Start-Service
Alias           sbp -> Set-PSBreakpoint
Alias           sc -> Set-Content
Alias           select -> Select-Object
Alias           set -> Set-Variable
Alias           shcm -> Show-Command
Alias           si -> Set-Item
Alias           sl -> Set-Location
Alias           sleep -> Start-Sleep
Alias           sls -> Select-String
Alias           sort -> Sort-Object
Alias           sp -> Set-ItemProperty
Alias           spjb -> Stop-Job
Alias           spps -> Stop-Process
Alias           spsv -> Stop-Service
Alias           start -> Start-Process
Alias           sujb -> Suspend-Job
Alias           sv -> Set-Variable
Alias           swmi -> Set-WMIInstance
Alias           tee -> Tee-Object
Alias           trcm -> Trace-Command
Alias           type -> Get-Content
Alias           wget -> Invoke-WebRequest
Alias           where -> Where-Object
Alias           wjb -> Wait-Job
Alias           write -> Write-Output


  
Creat a literal string
$animal = 'fox'
'The quick brown $animal jumps over the lazy dog.'
Details
Using the single quote (‘) will create a literal string, that will not evaluate any varibles
or expressions in the string. It will output exactly as entered.

Example
PS C:\> $animal = 'fox'
>> 'The quick brown $animal jumps over the lazy dog.'

The quick brown $animal jumps over the lazy dog.
  
Evaluate a Expression in a string
$animal = 'FOX'
"The quick brown $($animal.ToLower()) jumps over the lazy dog $(2 + 3) times."
Details
Use to insert a value in a string based on an expression

Example
PS C:\> $animal = 'FOX'
>> "The quick brown $($animal.ToLower()) jumps over the lazy dog $(2 + 3) times."

The quick brown fox jumps over the lazy dog 5 times.
  
Evaluate an Variable in a string
$animal = 'fox'
"The quick brown $animal jumps over the lazy dog."
Details
Use to insert a value in a string based on the variable

Example
PS C:\> $animal = 'fox'
>> "The quick brown $animal jumps over the lazy dog."

The quick brown fox jumps over the lazy dog.
  
Exclude blanks when splitting a string into an array
$option = [System.StringSplitOptions]::RemoveEmptyEntries
$string.split(";",$option)
Details
Uses a split and optional options to break apart the string

Example
PS C:\> $string = 'A1;B2;;D4'
>> $option = [System.StringSplitOptions]::RemoveEmptyEntries
>> $string.split(";",$option)

A1
B2
D4
  |  |  
Find characters between Brackets []
$string = "LastName, FirstName ([samaccountname])"
[Regex]::Matches($string, '(?<=\[)(.*?)(?=])').Value
Details
Uses a regex expression with find all characters that match the expression.

Example
PS C:\> $string = "LastName, FirstName ([samaccountname])"
>> [Regex]::Matches($string, '(?<=\[)(.*?)(?=])').Value

samaccountname
  |  
Find characters between double quotes “
$string = '"LastName, FirstName" ([samaccountname])'
[Regex]::Matches($string, '(?<=\")(.*?)(?=\")').Value
Details
Uses a regex expression with find all characters that match the expression.

Example
PS C:\> $string = '"LastName, FirstName" ([samaccountname])'
>> [Regex]::Matches($string, '(?<=\")(.*?)(?=\")').Value

LastName, FirstName
  |  
Find characters between Greater Than and Less Than signs
$string = "LastName,  ([samaccountname])"
[Regex]::Matches($string, '(?<=\<)(.*?)(?=\>)').Value
Details
Uses a regex expression with find all characters that match the expression.

Example
PS C:\> $string = "LastName,  ([samaccountname])"
>> [Regex]::Matches($string, '(?<=\<)(.*?)(?=\>)').Value

FirstName
  |  
Find characters between Parentheses ()
$string = "LastName, FirstName ([samaccountname])"
[Regex]::Matches($string, '(?<=\()(.*?)(?=\))').Value
Details
Uses a regex expression with find all characters that match the expression.

Example
PS C:\> $string = "LastName, FirstName ([samaccountname])"
>> [Regex]::Matches($string, '(?<=\()(.*?)(?=\))').Value

[samaccountname]
  |  
Find characters between single quotes ‘
$string = "'LastName',  ([samaccountname])"
[Regex]::Matches($string, "(?<=\')(.*?)(?=\')").Value
Details
Uses a regex expression with find all characters that match the expression.

Example
PS C:\> $string = "'LastName',  ([samaccountname])"
>> [Regex]::Matches($string, "(?<=\')(.*?)(?=\')").Value

LastName
  |  
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 Email Address in String
$emailRegex = @"
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?: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]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
"@
[Regex]::Matches($string, $emailRegex).Value
Details
This regex example will extract any and all email addresses from a string.

Example
PS C:\> $string = "My email is [email protected] but please don't spam me"
>> $emailRegex = @"
>> (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?: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]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
>> "@
>> [Regex]::Matches($string, $emailRegex).Value

[email protected]
  |  
Find Guid in String
$guidPattern = "(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}"
[Regex]::Matches($string, $guidPattern).Value
Details
Uses regex to return a Guid inside of a string.

Example
PS C:\> $string = 'Find the guid 1d10da0a-88b2-4129-93a1-e4e33edb3cc5 in this string.'
>> $guidPattern = "(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}"
>> [Regex]::Matches($string, $guidPattern).Value

1d10da0a-88b2-4129-93a1-e4e33edb3cc5
  |  
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 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 none ASCII characters
$string = "In mathematics, summation (capital Greek sigma symbol: ∑) is the addition of a sequence of numbers."
$string.ToCharArray() | ForEach-Object{
    $char = $_
    try{$ascii = [byte][char]$char}
    catch{"$char - None ascii character" }
}
Details
Breaks the string into a char array and attempts is convert the char back to it ASCII value.
If it fails to convert then it is not a standard ASCII character.
Output Example
PS C:\> ∑ - None ascii character
  
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 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 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/
  |  
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
  |  
Get all Text after a character
$string = "1. Computer name:DC-G11-FTW.contoso.com"
$string.Substring($string.IndexOf(":")+1,$string.length-$string.IndexOf(":")-1)
Details
Uses a substring and IndexOf to break apart the string

Example
PS C:\> $string = "1. Computer name:DC-G11-FTW.contoso.com"
>> $string.Substring($string.IndexOf(":")+1,$string.length-$string.IndexOf(":")-1)

DC-G11-FTW.contoso.com
  |  
HERE-STRINGS
$string = @"
"The quick brown fox jumps over the lazy dog"  is an English-language pangrama sentence
that contains all of the letters of the alphabet. It is commonly used for touch-typing
practice, testing typewriters and computer keyboards, displaying examples of fonts, and
other applications involving text where the use of all letters in the alphabet is desired.
Owing to its brevity and coherence, it has become widely known.
"@
$string
Details
Helpful for multiple lines, quotation marks, and all unknown strings
Use double quotes to evaluate variables and expressions and single quotes for literal

Example
PS C:\> $string = @"
>> "The quick brown fox jumps over the lazy dog"  is an English-language pangrama sentence
>> that contains all of the letters of the alphabet. It is commonly used for touch-typing
>> practice, testing typewriters and computer keyboards, displaying examples of fonts, and
>> other applications involving text where the use of all letters in the alphabet is desired.
>> Owing to its brevity and coherence, it has become widely known.
>> "@
>> $string

"The quick brown fox jumps over the lazy dog"  is an English-language pangrama sentence
that contains all of the letters of the alphabet. It is commonly used for touch-typing
practice, testing typewriters and computer keyboards, displaying examples of fonts, and
other applications involving text where the use of all letters in the alphabet is desired.
Owing to its brevity and coherence, it has become widely known.
  
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
  
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)
}
Remove all special characters including spaces
$string = "1. Computer name:DC-G11-FTW.contoso.com"
[regex]::Replace($string,"[^0-9a-zA-Z]","")
Details
Uses a regex expression with replace to remove all characters that do not match the expression.

Example
PS C:\> $string = "1. Computer name:DC-G11-FTW.contoso.com"
>> [regex]::Replace($string,"[^0-9a-zA-Z]","")

1ComputernameDCG11FTWcontosocom
  |  |  
Remove all special characters leaving spaces
$string = "1. Computer name:DC-G11-FTW.contoso.com"
[regex]::Replace($string,"[^0-9a-zA-Z ]","")
Details
Uses a regex expression with replace to remove all characters that do not match the expression.

Example
PS C:\> $string = "1. Computer name:DC-G11-FTW.contoso.com"
>> [regex]::Replace($string,"[^0-9a-zA-Z ]","")

1 Computer nameDCG11FTWcontosocom
  |  |  
Repeat a character
"ha" * 5
Details
This will repeat the given string by the number given

Example
PS C:\> "ha" * 5

hahahahaha
  
Repeat a character inside another string
"<$("#" * 5) Repeating character example $("#" * 5)>"
Details
This will repeat the given string by the number given inside of another string

Example
PS C:\> "<$("#" * 5) Repeating character example $("#" * 5)>"

<##### Repeating character example #####>
  
Replace using plain text replacement
$string = 'The quick brown fox jumps over the lazy dog'
$string.Replace("brown","red")
Details
Use the .Replace to Replace String using a plain text value

Example
PS C:\> $string = 'The quick brown fox jumps over the lazy dog'
>> $string.Replace("brown","red")

The quick red fox jumps over the lazy dog
  |  
Replace using Regular Expression
$string = 'Thank you for calling 555-5757 your order total is $123.32'
$string -Replace "[0-9]","0"
Details
Use the -Replace to Replace String using a regular expression

Example
PS C:\> $string = 'Thank you for calling 555-5757 your order total is $123.32'
>> $string -Replace "[0-9]","0"

Thank you for calling 000-0000 your order total is $000.00
  |  |  
Reverse a string
$array = $string.ToCharArray()
[array]::Reverse($array)
-join($array)

Details
Reserves the order of all characters in a string

Example
PS C:\> $string = "abcde"
>> $array = $string.ToCharArray()
>> [array]::Reverse($array)
>> -join($array)

edcba
  |  
Split string into an array on spaces
$string = "The quick brown fox jumps over the lazy dog."
$string.split()
Details
Uses a split to break apart the string

Example
PS C:\> $string = "The quick brown fox jumps over the lazy dog."
>> $string.split()

The
quick
brown
fox
jumps
over
the
lazy
dog.
  |  |  
String to lowercase
$string.ToLower()
Details
String to lowercase

Example
PS C:\> $string = "WILLIAMS"
>> $string.ToLower()

williams
  
String to uppercase
$string.ToUpper()
Details
String to uppercase

Example
PS C:\> $string = "williams"
>> $string.ToUpper()

WILLIAMS
  
String to uppercase on first letter only
$string.Substring(0,1).ToUpper() + $string.Substring(1,$string.Length-1).ToLower()
Details
String to uppercase on first letter only

Example
PS C:\> $string = "williams"
>> $string.Substring(0,1).ToUpper() + $string.Substring(1,$string.Length-1).ToLower()

Williams
  
Use StringBuilder to combine multiple strings
$stringBuilder = New-Object System.Text.StringBuilder
for ($i = 0; $i -lt 10; $i++){
    $stringBuilder.Append("Line $i`r`n") | Out-Null
}
$stringBuilder.ToString()
Details
This performs much better the stanard array builder of +=

Example
PS C:\> $stringBuilder = New-Object System.Text.StringBuilder
>> for ($i = 0; $i -lt 10; $i++){
>>     $stringBuilder.Append("Line $i`r`n") | Out-Null
>> }
>> $stringBuilder.ToString()

Line 0
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9