functions/string/Remove-PSUString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function Remove-PSUString
{
<#
    .SYNOPSIS
        Trims a string.
     
    .DESCRIPTION
        Trims a string.
     
    .PARAMETER InputString
        The string that will be trimmed
     
    .PARAMETER What
        What should be trimmed?
     
    .PARAMETER Start
        Should only the start be trimmed?
     
    .PARAMETER End
        Should only the end be trimmed?
     
    .EXAMPLE
        Get-Content file.txt | trim
     
        Retrieves all content from file.txt, then trims each line.
#>

    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true)]
        [string[]]
        $InputString,
        
        [Parameter(Position = 0)]
        [string]
        $What = "",
        
        [switch]
        $Start,
        
        [switch]
        $End
    )
    
    begin { }
    process
    {
        foreach ($line in $InputString)
        {
            if ($Start -and (-not $End)) { $line.TrimStart($What) }
            elseif ((-not $Start) -and $End) { $line.TrimEnd($What) }
            else { $line.Trim($What) }
        }
    }
    end { }
}
Import-PSUAlias -Name trim -Command Remove-PSUString