Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Last active September 5, 2021 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techthoughts2/bc0b1a038bd7bc99c6e9 to your computer and use it in GitHub Desktop.
Save techthoughts2/bc0b1a038bd7bc99c6e9 to your computer and use it in GitHub Desktop.
String Manipulation
#replace
if($vDisk -contains " "){
$vDisk = $vDisk -replace(" ",",")
}
#remove last character
$string.Substring(0,$string.Length-1)
#extract a specific string set from a long string
[regex]$r = "(?<=Vlan).*?(?= )"
$r.matches($nativeVLANString) | `
ForEach-Object {
$nativeVLANStringFinal = $_.Value
}
#match airport code
if ($APC -match "^[A-Z]{3}$") {
#correct
}
#splitting a string up at a certain character
$split = $switch.display_name.split("[")
$switchname = $split[0] + ".$region"
#another example
$config.Hostname = ($corePull.result.name.split("."))[0]
#reduce string to a certain length
if($compName.length -gt 6){
$compName = $compName.substring(0,6)
}
#get all the object things and put them into a list serparated by commas
$secondaryDisks = $t.'VM-Object'.DataDisks
($secondaryDisks | Select-Object -ExpandProperty DiskSize) -join ","
# get last character of a string
$a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$a.substring($a.length - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment