Functions/Remove-Trailing.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
filter Remove-Trailing {
<#
.SYNOPSIS
    Removes trailing spaces from a string or array of strings.
.DESCRIPTION
    Removes trailing spaces from a string or array of strings.
.PARAMETER String
    A string or array of strings that may have trailing spaces. Can also accept input from the pipeline.
.EXAMPLE
    "[$(Remove-Trailing -String 'This has trailing spaces ')]"
 
    [This has trailing spaces]
#>


    #region Parameter
    [CmdletBinding(ConfirmImpact='None')]
    [OutputType([string[]])]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions','')]
    Param(
        [Parameter(Mandatory, HelpMessage = 'Enter a string, even with trailing spaces', Position = 0, ValueFromPipeline)]
        [string[]] $String
        )
    #endregion Parameter

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
    }

    process {
        foreach ($s in $String) {
            $s | out-string | foreach-object { ($_).TrimEnd() }
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }
}