Src/ConvertTo-Base64.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
function ConvertTo-Base64 {
    <#
    .SYNOPSIS
    Converts a UTF string to Base64

    .DESCRIPTION
    Delegates to the underlying System.Text.Encoding and System.Convert classes
    to convert the UTF8 input string to a byte[] and then back to Base64

    .PARAMETER String
    Specifies the UTF8 string value

    .INPUTS
    System.String

    .OUTPUTS
    System.String

    .EXAMPLE
    PS> ConvertTo-Base64 -String 'test string'
    'dGVzdCBzdHJpbmc='

    .EXAMPLE
    PS> 'test string' | ConvertFrom-Base64
    'dGVzdCBzdHJpbmc='

    .LINK
    https://github.com/charliebillen/ConvertBase64Strings
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param (
        [Parameter(Mandatory, ValueFromPipeline)]
        [string]
        $String
    )

    process {
        $bytes = [System.Text.Encoding]::UTF8.GetBytes($String)
        [Convert]::ToBase64String($bytes)
    }
}