PowerShell and Strings

Here is a memento of several ways to play with strings in PowerShell.

  • Insert Double Quote and ASCII codes
  • Return a specified number of characters


Insert Double Quotes and ASCII Codes

To insert double quotes in a string, the following ASCII code can be used: [char]34

ASCII codes can be inserted like follow: $([char]34)

For example, using the UserName environment variable:

$User = "My UserName is $([char]34)$env:UserName$([char]34)"
Write-Host $User



Return a specified number of characters in a string

Here is the equivalent function of the VBS function “MID

Write-Host 'Getting the site code from the location code'


$SiteCode = $LocationCode.substring(7,2)

Write-Host "The site code is $SiteCode"

If ($SiteCode -eq 'GE') {
	Write-Host "The computer is located in Geneva"

Else {
	Write-Host "The computer is not located in Geneva"