Functions/GenXdev.Windows/CurrentUserHasElevatedRights.ps1
<##############################################################################
Part of PowerShell module : GenXdev.Windows Original cmdlet filename : CurrentUserHasElevatedRights.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 Checks if the current user has elevated rights. .DESCRIPTION Determines whether the current Windows user has administrative or backup operator privileges by checking their security principal roles. Returns true if the user has elevated rights, false otherwise. Implements robust error handling for security and access-related issues. .OUTPUTS System.Boolean Returns true if user has admin or backup operator rights, false otherwise. .EXAMPLE $hasRights = CurrentUserHasElevatedRights ###############################################################################> function CurrentUserHasElevatedRights { [CmdletBinding()] [OutputType([System.Boolean])] param() begin { # store original error preferences for restoration $originalEAP = $ErrorActionPreference $originalErrorView = $ErrorView # set strict error handling $ErrorActionPreference = 'Stop' $ErrorView = 'DetailedView' Microsoft.PowerShell.Utility\Write-Verbose "Checking current user's security privileges..." } process { try { # get the current windows identity with error handling $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() # create a new principal object from the identity $principal = Microsoft.PowerShell.Utility\New-Object ` -TypeName System.Security.Principal.WindowsPrincipal ` -ArgumentList $identity # check for administrative or backup operator privileges if ($principal.IsInRole( [System.Security.Principal.WindowsBuiltInRole]::Administrator) -or $principal.IsInRole( [System.Security.Principal.WindowsBuiltInRole]::BackupOperator)) { Microsoft.PowerShell.Utility\Write-Verbose 'User has elevated rights' return $true } Microsoft.PowerShell.Utility\Write-Verbose 'User does not have elevated rights' return $false } catch [System.Security.SecurityException] { Microsoft.PowerShell.Utility\Write-Error ` -Message 'Security violation checking user rights' ` -Exception $_.Exception ` -Category SecurityError ` -ErrorId 'SecurityViolation' throw } catch [System.UnauthorizedAccessException] { Microsoft.PowerShell.Utility\Write-Error ` -Message 'Access denied while verifying user privileges' ` -Exception $_.Exception ` -Category PermissionDenied ` -ErrorId 'AccessDenied' throw } catch { Microsoft.PowerShell.Utility\Write-Error ` -Message 'Unexpected error during rights verification' ` -Exception $_.Exception ` -Category OperationStopped ` -ErrorId 'UnexpectedError' throw } } end { # restore original error handling settings $ErrorActionPreference = $originalEAP $ErrorView = $originalErrorView } } |