IsElevated.psm1
<#
.Synopsis Check if Powershell Console is running in elevated mode. .DESCRIPTION Checking if Powershell Console is running in elevated mode. .EXAMPLE Get-ConsoleElevationStatus Name : Windows PowerShell ISE Host Version : 5.1.17134.407 InstanceId : e520b701-6509-498c-9727-22336d10f0a4 UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-US CurrentUICulture : en-US PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions DebuggerEnabled : True IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace IsElevated : True .EXAMPLE iselevated -short True #> function Get-ConsoleElevationStatus { [CmdletBinding(SupportsShouldProcess=$true, PositionalBinding=$false, ConfirmImpact='Medium')] [Alias('iselevated')] [OutputType([String])] Param ( # Switch to return only bool value [Alias('short')] [switch] $Bool ) Begin { } Process { if ($pscmdlet.ShouldProcess("Powershell Console")) { $IsElevated=(whoami /all | select-string S-1-16-12288) -ne $null if($Bool){ Write-Verbose "Returning only boolean value - $true if elevated and $false otherwise" $IsElevated }else{ Write-Verbose "Returning full info about Powershell Console including Elevation Status" $Props=@{ CurrentCulture="$($Host.CurrentCulture)" CurrentUICulture="$($Host.CurrentUICulture)" DebuggerEnabled="$($Host.DebuggerEnabled)" InstanceId="$($Host.InstanceId)" IsRunspacePushed="$($Host.IsRunspacePushed)" Name="$($Host.Name)" PrivateData="$($Host.PrivateData)" Runspace="$($Host.Runspace)" UI="$($Host.UI)" Version="$($Host.Version)" IsElevated=$IsElevated } $ConsoleHostObj=New-Object -TypeName psobject -Property $Props $ConsoleHostObj | Select-Object Name, Version, InstanceID, UI, CurrentCulture,CurrentUICulture, PrivateData, DebuggerEnabled, IsRunspacePushed, Runspace, IsElevated } } } End { } } |