Public/Remove-VarStash.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
57
58
59
60
<#
.SYNOPSIS
   Removes a variable stash.
.DESCRIPTION
   Permanently removes a variable stash.
.EXAMPLE
   Remove-VarStash -Name MyStash
 
   Removes the stash named MyStash.
.EXAMPLE
   Remove-VarStash -Index 2
 
   Removes the stash with index position 2 (lates stash has index 0).
.EXAMPLE
   Remove-VarStash -Index 2 -WhatIf
 
   Shows what would have happened if this command would run without the -WhatIf parameter.
#>

function Remove-VarStash {
    [CmdletBinding(DefaultParameterSetName = 'Name')]
    param(
        [parameter(Mandatory,
                   ParameterSetName = 'Name',
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [parameter(Mandatory,
                   ParameterSetName = 'Index',
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(0, [int]::MaxValue)]
        [int]$Index,

        [parameter(ParameterSetName = 'Name')]
        [parameter(ParameterSetName = 'Index')]
        [switch]$WhatIf
    )
    
    Process {
        
        switch ($PSCmdlet.ParameterSetName) {
            'Name' { 
                $Stash = Get-VarStash -Name $Name -ErrorAction Stop
            }
            'Index' { 
                $Stash = Get-VarStash -Index $Index -ErrorAction Stop
            }
        }

        try {
            Remove-Item -Path "$env:APPDATA\VariableStash\VariableStash_$($Stash.Name).xml" -WhatIf:$WhatIf
        }
        catch {
            Throw "Unable to remove stash. Error: $_"
        }

    }
}