Functions/GenXdev.Data.Preferences/Remove-GenXdevPreference.ps1
<##############################################################################
Part of PowerShell module : GenXdev.Data.Preferences Original cmdlet filename : Remove-GenXdevPreference.ps1 Original author : René Vaessen / GenXdev Version : 1.264.2025 ################################################################################ MIT License Copyright 2021-2025 GenXdev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ################################################################################> ############################################################################### <# .SYNOPSIS Removes a preference value from the GenXdev preferences store. .DESCRIPTION This function removes a preference value from both the local store and optionally from the default store. It provides two parameter sets - one for local removal only and another for removing from both local and default stores. The function ensures proper synchronization when modifying the default store. .PARAMETER Name Specifies the name of the preference to remove. This is required and can be provided via pipeline input. .PARAMETER RemoveDefault When specified, removes the preference from both the local and default stores. If not specified, only removes from the local store. .PARAMETER SessionOnly Use alternative settings stored in session for Data preferences like Language, Database paths, etc. .PARAMETER ClearSession Clear the session setting (Global variable) before retrieving. .PARAMETER PreferencesDatabasePath Database path for preference data files. Alias: DatabasePath .PARAMETER SkipSession Dont use alternative settings stored in session for Data preferences like Language, Database paths, etc. .EXAMPLE Remove-GenXdevPreference -Name "Theme" Removes the "Theme" preference from the local store only. .EXAMPLE removePreference "Theme" -RemoveDefault Removes the "Theme" preference from both local and default stores. #> function Remove-GenXdevPreference { [CmdletBinding( SupportsShouldProcess = $true )] [Alias('removePreference')] param( ####################################################################### [Parameter( Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The name of the preference to remove' )] [ValidateNotNullOrEmpty()] [string]$Name, ####################################################################### [Parameter( Mandatory = $false, Position = 1, ParameterSetName = 'All', HelpMessage = 'Switch to also remove the preference from defaults' )] [switch]$RemoveDefault, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Use alternative settings stored in session for ' + 'Data preferences like Language, Database paths, etc') )] [Alias('DatabasePath')] [string]$PreferencesDatabasePath, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Use alternative settings stored in session for ' + 'Data preferences like Language, Database paths, etc') )] [switch]$SessionOnly, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Clear the session setting (Global variable) ' + 'before retrieving') )] [switch]$ClearSession, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Dont use alternative settings stored in session ' + 'for Data preferences like Language, Database ' + 'paths, etc') )] [Alias('FromPreferences')] [switch]$SkipSession ####################################################################### ) begin { # copy identical parameter values to prepare for database path lookup $params = GenXdev.Helpers\Copy-IdenticalParamValues ` -BoundParameters $PSBoundParameters ` -FunctionName 'GenXdev.Data\Get-GenXdevPreferencesDatabasePath' ` -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable ` -Scope Local ` -ErrorAction SilentlyContinue) # resolve the actual database path using the helper function $PreferencesDatabasePath = GenXdev.Data\Get-GenXdevPreferencesDatabasePath @params # create global variable name for this preference $globalVariableName = "GenXdevPreference_$Name" # output verbose information about the database path being used Microsoft.PowerShell.Utility\Write-Verbose ` "Using database path: $PreferencesDatabasePath" # output verbose information about the operation start Microsoft.PowerShell.Utility\Write-Verbose ('Starting preference ' + "removal for: $Name") } process { # handle clearing session variables first if requested if ($ClearSession) { # confirm the operation with the user before proceeding if ($PSCmdlet.ShouldProcess($Name, 'Clear session variable')) { # clear the session variable Microsoft.PowerShell.Utility\Remove-Variable -Name $globalVariableName -Scope Global -ErrorAction SilentlyContinue } } # handle session-only removal if ($SessionOnly) { # confirm the operation with the user before proceeding if ($PSCmdlet.ShouldProcess($Name, 'Remove session-only preference')) { # remove the preference from session-only store GenXdev.Data\Remove-KeyFromStore -Name $Name -SessionOnly } } # check if the operation should proceed based on whatif/confirm if ($PSCmdlet.ShouldProcess($Name, 'Remove preference')) { # clear session variable for this preference if not SkipSession if (-not $SkipSession) { Microsoft.PowerShell.Utility\Remove-Variable -Name $globalVariableName -Scope Global -ErrorAction SilentlyContinue } # output verbose information about local store removal Microsoft.PowerShell.Utility\Write-Verbose ('Removing preference ' + $Name + ' from local store') # copy identical parameter values for Remove-KeyFromStore $removeLocalParams = GenXdev.Helpers\Copy-IdenticalParamValues ` -BoundParameters $PSBoundParameters ` -FunctionName 'GenXdev.Data\Remove-KeyFromStore' ` -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable ` -Scope Local ` -ErrorAction SilentlyContinue) # assign specific parameters for local store removal $removeLocalParams.StoreName = 'GenXdev.PowerShell.Preferences' $removeLocalParams.KeyName = $Name $removeLocalParams.SynchronizationKey = 'Local' $removeLocalParams.DatabasePath = $PreferencesDatabasePath # remove the preference key from the local store GenXdev.Data\Remove-KeyFromStore @removeLocalParams # handle default store removal if requested if ($RemoveDefault) { Microsoft.PowerShell.Utility\Write-Verbose ('Removing preference ' + $Name + ' from default store') # copy identical parameter values for Remove-KeyFromStore (defaults) $removeDefaultParams = GenXdev.Helpers\Copy-IdenticalParamValues ` -BoundParameters $PSBoundParameters ` -FunctionName 'GenXdev.Data\Remove-KeyFromStore' ` -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable ` -Scope Local ` -ErrorAction SilentlyContinue) # assign specific parameters for default store removal $removeDefaultParams.StoreName = 'GenXdev.PowerShell.Preferences' $removeDefaultParams.KeyName = $Name $removeDefaultParams.SynchronizationKey = 'Defaults' $removeDefaultParams.DatabasePath = $PreferencesDatabasePath # remove the preference key from the default store GenXdev.Data\Remove-KeyFromStore @removeDefaultParams } } } end { } } ############################################################################### |