commands.ps1

function Get-ExplorerFolder
{
<#
    .SYNOPSIS
        Returns the current explorer folder configuration items.
     
    .DESCRIPTION
        Returns the current explorer folder configuration items.
     
    .PARAMETER Name
        The name to search by
     
    .EXAMPLE
        PS C:\> Get-ExplorerFolder
     
        Returns all folders and their settings.
     
    .EXAMPLE
        PS C:\> Get-ExplorerFolder -Name Desktop
     
        Returns the status of the desktop folder.
#>

    [CmdletBinding()]
    Param (
        [string]
        $Name = "*"
    )
    process
    {
        Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\ | Get-ItemProperty | Select-PSFObject @(
            'Name',
            'PSChildName as ID to Guid',
            'PSChildName as IDString'
        ) -ScriptProperty @{
            "IsDefined" = {
                if (-not (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration")) { return $false }
                ((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration").PSObject.Properties.Name -contains $this.IDString)
            }
            "Enabled"   = {
                if (-not (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration")) { return $false }
                [bool]((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration").$($this.IDString))
            }
        } -TypeName 'ExplorerFolder.FolderSetting' | Where-Object Name -Like $Name
    }
}

function Remove-ExplorerFolder
{
<#
    .SYNOPSIS
        Removes a registered named folder from the list of shown folders.
     
    .DESCRIPTION
        Removes a registered named folder from the list of shown folders.
        Use '-Force' to remove the overall whitelisting architecture.
     
    .PARAMETER Name
        Name of the folder to unlist.
     
    .PARAMETER Force
        Purge the entire registration key.
     
    .EXAMPLE
        PS C:\> Remove-ExplorerFolder -Name Desktop
         
        Removes the registration of the Desktop folder.
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]
        $Name,
        
        [switch]
        $Force
    )
    
    begin
    {
        if (-not (Test-PSFPowerShell -Elevated))
        {
            Stop-PSFFunction -Message "This command requires elevation" -EnableException $EnableException -Cmdlet $PSCmdlet
            return
        }
        
        $allItems = Get-ExplorerFolder
    }
    process
    {
        if (Test-PSFFunctionInterrupt) { return }
        
        foreach ($nameItem in $Name)
        {
            $item = $allItems | Where-Object Name -EQ $nameItem
            if (-not $item) { continue }
            if (-not $item.IsDefined) { continue }
            
            Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration" -Name $item.IDString
        }
    }
    end
    {
        if (Test-PSFFunctionInterrupt) { return }
        
        if ($Force)
        {
            if (-not ($allItems | Where-Object IsDefined))
            {
                Remove-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration" -Force
            }
        }
    }
}

function Set-ExplorerFolder
{
<#
    .SYNOPSIS
        Enables and disables folders to be displayed in the explorer shell.
     
    .DESCRIPTION
        Enables and disables folders to be displayed in the explorer shell.
     
    .PARAMETER Name
        Name of the folder to show or hide
     
    .PARAMETER Disable
        Disable it, rather than show it (default).
        Useful when enabling an entire category but selectively hiding members.
     
    .PARAMETER EnableException
        Replaces user friendly yellow warnings with bloody red exceptions of doom!
        Use this if you want the function to throw terminating errors you want to catch.
     
    .EXAMPLE
        PS C:\> Set-ExplorerFolder -Name "Desktop"
     
        Enables the desktop folder to be shown in the desktop shell
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]
        $Name,
        
        [switch]
        $Disable,
        
        [switch]
        $EnableException
    )
    
    begin
    {
        if (-not (Test-PSFPowerShell -Elevated))
        {
            Stop-PSFFunction -Message "This command requires elevation" -EnableException $EnableException -Cmdlet $PSCmdlet
            return
        }
        
        $folders = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\ | Get-ItemProperty | Select-Object Name, PSChildName
    }
    process
    {
        if (Test-PSFFunctionInterrupt) { return }
        
        foreach ($nameItem in $Name)
        {
            $folder = $folders | Where-Object Name -EQ $nameItem
            if (-not $folder)
            {
                Stop-PSFFunction -Message "Could not resolve folder $nameItem!" -EnableException $EnableException -Cmdlet $PSCmdlet -Continue
            }
            
            if (-not (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration"))
            {
                $null = New-Item -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer -Name AllowedEnumeration
            }
            
            if ($Disable) { Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration" -Name $folder.PSChildName -Value 0 }
            else { Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AllowedEnumeration" -Name $folder.PSChildName -Value 1 }
        }
    }
}