Functions/GenXdev.Windows/Set-TaskbarAlignment.ps1

<##############################################################################
Part of PowerShell module : GenXdev.Windows
Original cmdlet filename : Set-TaskbarAlignment.ps1
Original author : René Vaessen / GenXdev
Version : 1.300.2025
################################################################################
Copyright (c) René Vaessen / GenXdev
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
################################################################################>

###############################################################################
<#
.SYNOPSIS
Configures Windows 11+ taskbar alignment between center and left positions.
 
.DESCRIPTION
Sets the taskbar alignment in Windows 11 and newer versions by modifying the
registry key 'TaskbarAl' under HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\
Explorer\Advanced. The alignment can be set to either center (value 1) or left
(value 0).
 
.PARAMETER Justify
Specifies the desired taskbar alignment: 'Center' or 'Left'. This setting is
converted to the appropriate registry value (1 for Center, 0 for Left).
 
.EXAMPLE
Set-TaskbarAlignment -Justify Left
Sets the Windows 11 taskbar to left alignment
 
.EXAMPLE
Set-TaskAlign Center -WhatIf
Shows what would happen if taskbar was set to center alignment
#>

function Set-TaskbarAlignment {

    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        ########################################################################
        [Parameter(
            Mandatory = $true,
            Position = 0,
            HelpMessage = 'The taskbar alignment (Center or Left)'
        )]
        [ValidateSet('Center', 'Left')]
        [string] $Justify
        ########################################################################
    )

    begin {

        # store the registry path for taskbar settings
        $regPath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced'

        # log the requested alignment change for troubleshooting
        Microsoft.PowerShell.Utility\Write-Verbose "Setting taskbar alignment to: $Justify"
    }


    process {

        # convert the alignment choice to its corresponding registry value
        $value = if ($Justify -eq 'Left') { 0 } else { 1 }

        # check if we should proceed with the registry modification
        if ($PSCmdlet.ShouldProcess(
                'Windows Taskbar Alignment',
                "Set alignment to $Justify"
            )) {

            # update the registry key
            $null = Microsoft.PowerShell.Management\Set-ItemProperty -Path $regPath `
                -Name 'TaskbarAl' `
                -Value $value

            Microsoft.PowerShell.Utility\Write-Verbose "Registry value 'TaskbarAl' set to: $value"
        }
    }

    end {
    }
}