Enter-Elevated.ps1
|
<#PSScriptInfo
.VERSION 0.1.1 .GUID f7cead90-1b3c-49d7-b18a-bac381ad0eb3 .AUTHOR Wallby .COPYRIGHT Copyright (c) 2026 Wallby 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. .TAGS PowerShell Windows Administration RunAs Terminal wt.exe WindowsTerminal.exe .PROJECTURI https://www.powershellgallery.com/packages/enter-elevated .RELEASENOTES [Version 0.1.1] - Removed a leftover uncommented pause statement used during development. #> <# .SYNOPSIS As if elevating the current terminal tab. .DESCRIPTION Open the same working directory in a new administrator window. Then close the old non-administrator tab/window. .PARAMETER Path Specify a (relative) path to open. If not specified the current working directory ($PWD) is used. .INPUTS Path .LINK Project Site: https://www.powershellgallery.com/packages/enter-elevated .LINK Start-Process about_PowerShell_exe #> using namespace System.Diagnostics using namespace System.IO param( [Parameter(ValueFromPipeline=$true)] [string]$Path=$PWD ) if(!(Test-Path $Path)) { $ScriptName = ($MyInvocation.MyCommand.Name).TrimEnd('.ps1') throw [DirectoryNotFoundException]::new("$($ScriptName): Cannot find path '$Path' because it does not exist.", $Path) } # Make path absolute so that it can be passed to $Path = (Resolve-Path $Path).Path function private:Add-TypeIf { param( [string]$TypeName, [string]$Source ) if(-not ($TypeName -as [type])) { Add-Type $Source } } Add-TypeIf Win32 @" using System; using System.Runtime.InteropServices; public class Win32 { [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; } [DllImport("dwmapi.dll", EntryPoint = "DwmGetWindowAttribute")] private static extern int _DwmGetWindowAttribute(IntPtr hWnd, int dwAttribute, IntPtr pvAttribute, int cbAttribute); public enum DWMWINDOWATTRIBUTE { ExtendedFrameBounds = 9 } [return: MarshalAs(UnmanagedType.Bool)] public static int DwmGetWindowAttribute<T>(IntPtr hWnd, DWMWINDOWATTRIBUTE dwAttribute, out T pvAttribute) { int size = Marshal.SizeOf(typeof(T)); IntPtr mem = Marshal.AllocHGlobal(size); int hresult = _DwmGetWindowAttribute(hWnd, (int)dwAttribute, mem, size); pvAttribute = Marshal.PtrToStructure<T>(mem); Marshal.FreeHGlobal(mem); return hresult; } } "@ $process = Get-Process -Id $PID [process]$wtProcess | Out-Null $parentPid = $PID do { # Should end up become the wt process $parentCi = Get-CimInstance Win32_Process -Filter "ProcessId LIKE `"$parentPid`"" if($parentCi.Name -eq 'WindowsTerminal.exe') { $wtProcess = Get-Process -Id $parentPid break } $parentPid = $parentCi.ParentProcessId #Write-Host "Parent ID: $parentPid ($($parentCi.Name))" #pause if($null -eq $parentPid) { throw 'WindowsTerminal.exe not found' } } while($true) $hwnd = $wtProcess.MainWindowHandle $rect = New-Object Win32+RECT # Using DwmGetWindowAttribute, not GetWindowRect, as testing proved that # passing the window rect to the wt.exe --pos argument results in an incorrect # position $hresult = [Win32]::DwmGetWindowAttribute($hwnd, [Win32+DWMWINDOWATTRIBUTE]::ExtendedFrameBounds, [ref]$rect) if($hresult -ne 0) { throw "Failed to obtain window rect: HR $hresult" } Start-Process 'wt' -Verb RunAs -ArgumentList '--pos',"$($rect.Left),$($rect.Top))",'powershell','-NoExit','-Command',"cd '$Path'" $host.SetShouldExit(0) |