Created
January 27, 2026 16:03
-
-
Save Rapidhands/bf897c3fe0d750134c2ef4b04d3d001d to your computer and use it in GitHub Desktop.
Function Self documented to Test if a script is Running in RunAsAdmin mode and Self elevate if necessary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Test-IsAdminAndElevate | |
| { | |
| <# | |
| .SYNOPSIS | |
| Tests if PowerShell is running as administrator and elevates privileges if needed. | |
| .DESCRIPTION | |
| This function checks if the current PowerShell session is running with administrator privileges. | |
| If not, it attempts to start a new elevated PowerShell session. | |
| Note that this function is built for Windows Powershell 5.1 and may not work in other environments. | |
| .EXAMPLE | |
| Test-IsAdminAndElevate | |
| Checks admin privileges and elevates if necessary. | |
| .EXAMPLE | |
| Get-Help Test-IsAdminAndElevate -ShowWindow | |
| Full Help about the function in a separate Windows | |
| .INPUTS | |
| None | |
| .OUTPUTS | |
| System.Object | |
| .NOTES | |
| Author: Olivier FERRIERE | |
| Date: 2024-12-30 | |
| Version: 1.0 | |
| Change : v1.0 2024-30-12 - Initial version | |
| v1.1 2024-30-12 - Added -Verbose and -WhatIf support and output type. | |
| Use ShouldProcess to confirm elevation. | |
| Use Write-Information instead of Write-Host. | |
| Use begin, process, and end blocks. | |
| Type cast variables for clarity. | |
| #> | |
| [CmdletBinding(SupportsShouldProcess)] | |
| [OutputType([bool])] | |
| param() | |
| begin | |
| { | |
| Write-Verbose -Message 'Initializing administrator check...' | |
| } | |
| process | |
| { | |
| try | |
| { | |
| Write-Verbose -Message 'Checking administrative privileges...' | |
| [Security.Principal.WindowsPrincipal]$Script:windowsPrincipal = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() | |
| [bool]$Script:isAdministrator = $Script:windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
| if ($Script:isAdministrator) | |
| { | |
| Write-Information 'PowerShell is already running as administrator.' | |
| $true | |
| } | |
| else | |
| { | |
| Write-Warning 'PowerShell is not running as administrator. Attempting to elevate...' | |
| [hashtable]$Script:processStartInfo = @{ | |
| FilePath = 'powershell.exe' | |
| Verb = 'runAs' | |
| ArgumentList = '-NoExit', '-ExecutionPolicy Bypass', "-Command `"Write-Host 'New elevated PowerShell session started.' -ForegroundColor Green`"" | |
| } | |
| if ($PSCmdlet.ShouldProcess('PowerShell', 'Elevate to administrator privileges')) | |
| { | |
| try | |
| { | |
| Start-Process @Script:processStartInfo | |
| $true | |
| } | |
| catch | |
| { | |
| Write-Error "Failed to start elevated session: $_" | |
| $false | |
| } | |
| } | |
| } | |
| } | |
| catch | |
| { | |
| Write-Error "An error occurred while checking/elevating privileges: $_" | |
| $false | |
| } | |
| } | |
| end | |
| { | |
| Write-Verbose -Message 'Completed administrator privilege check and elevation attempt.' | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment