Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
Last active October 23, 2018 13:51
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 mattmcnabb/223ff8d3dd2ec75c65514aec3c535bee to your computer and use it in GitHub Desktop.
Save mattmcnabb/223ff8d3dd2ec75c65514aec3c535bee to your computer and use it in GitHub Desktop.
Add a Truncate method to system.string
$Script = {
param([int]$MaxLength)
switch ($this)
{
{[string]::IsNullOrEmpty($_)} {$this; break}
{$_.Length -le $MaxLength} {$this; break}
default {$_.SubString(0, $MaxLength)}
}
}
$TypeDataSplat = @{
TypeName = 'System.String'
MemberType = 'ScriptMethod'
MemberName = 'Truncate'
Value = $Script
}
Update-TypeData @TypeDataSplat -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment