Remove-Macro.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
function Remove-Macro
{
    <#
    .Synopsis
        Removes Macros
    .Description
        Removes Macros by name. To remove all macros, pipe Get-Macro | Remove-Macro
    .Link
        Add-Macro
    .Link
        Get-Macro
    .Link
        Import-Macro
    .Link
        Remove-Macro
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
    # The name of the macro.
    [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
    [string[]]
    $Name,

    # The macro prefix.
    # The default prefix is an underscore _.
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $Prefix = '_'
    )

    process {
        $macroModules = Get-Macro @PSBoundParameters

        foreach ($lm in $macroModules) {
            #region Unload Each Module

            # Because we want to be thorough, and modules don't always clean up their aliases,
            foreach ($exportedAlias in $lm.ExportedAliases.Values) {
                if (Test-Path "alias:$($exportedAlias.Name)") { # remove each alias,
                    Remove-Item "alias:$($exportedAlias.Name)" -ErrorAction SilentlyContinue
                }
            }
            foreach ($exportedFunction in $lm.ExportedFunctions.Values) {
                if (Test-Path "function:$($exportedFunction.Name)") { # then remove each function,
                    Remove-Item "function:$($exportedFunction.Name)" -ErrorAction SilentlyContinue
                }
            }
            # then unload the module
            $lm | Remove-Module -ErrorAction SilentlyContinue
            #endregion Unload Each Module
        }
    }
}