There is no built in method to return the RIght N characters of a string, but we can use .SubString().
To return the rightmost 5 characters, we start at the character (5 less than the string length) and count 5 characters:
$var = "Hello world"
$result = $var.SubString($var.length - 5, 5)
$result
However this assumes the string will always be at least 5 characters long to start with. To make this more robust the number of characters can be restrained to be no more than the initial length of the string using [math]::min()
To return the rightmost 50 characters:
$var = "Hello world"
$startchar = [math]::min($var.length - 50,$var.length)
$startchar = [math]::max(0, $startchar)
$length = [math]::min($var.length, 50)
$var.SubString($startchar ,$length)
If the string is shorter than 50 characters, the above will display all the available characters rather than throw an error.
For quick re-use this can be placed into a function:
Function right { [CmdletBinding()] Param ( [Parameter(Position=0, Mandatory=$True,HelpMessage="Enter a string of text")] [String]$text, [Parameter(Mandatory=$True)] [Int]$Length ) $startchar = [math]::min($text.length - $Length,$text.length)
$startchar = [math]::max(0, $startchar)
$right = $text.SubString($startchar ,[math]::min($text.length, $Length)) $right } Example: PS C:\> right "hello world" -length 5
“We look forward to the time when the Power of Love will replace the Love of Power.
Then will our world know the blessings of peace” ~ William Gladstone
Left - Use $var.SubString(0,5) to return the first 5 characters of $var.
Methods - ToUpper(), PadRight(), Split(), Substring(), Replace() etc.