6

does anyone know how to disable quick edit mode from within a powershell script? This question's "answer" is not an answer:

Enable programmatically the "Quick Edit Mode" in PowerShell

(Though one could set the registry setting programatically, doing so would not affect the current session).

I have looked at the $host, $host.UI and $host.UI.RawUI objects and cannot find anything relevant.

To make things a bit more clear, I do not want to change the registry. In particular, I do not want to change the default behaviour. In fact, there is just one script, and really only one branch of the script, where I need to disable quick-edit. So I need to be able to disable it programatically. Or at the very least, be able to start powershell with command line options to disable quick edit.

Thanks.

Community
  • 1
  • 1
David I. McIntosh
  • 2,038
  • 4
  • 23
  • 45
  • @AnsgarWiechers Can you give me a bit more information? How do you know this? For example, is this simply because the relevant object properties have not been exposed, or is it a fundamental issue with how IO is done in a shell window? Thanks. – David I. McIntosh Jun 17 '15 at 18:58
  • To my knowledge `$host.UI.RawUI` is the only way to change console properties on the fly from within PowerShell. And that object simply doesn't provide a property or method to enable/disable QuickEdit. – Ansgar Wiechers Jun 17 '15 at 19:17
  • 1
    See https://stackoverflow.com/a/76855923/15016163 – Maicon Mauricio Aug 09 '23 at 01:49
  • @MaiconMaurico Thanks Maicon. [Comment above](https://stackoverflow.com/questions/30872345/script-commands-to-disable-quick-edit-mode?noredirect=1#comment135505082_30872345) gives Python code equavalent to the c# code in the [answer below](https://stackoverflow.com/a/42792718/1082063). Given that c# code can be complied dynamically in PowerShell, this also provides Powershell functionality. – David I. McIntosh Aug 25 '23 at 18:10

3 Answers3

6

I hope not too late.

This solution based on C#, but it does not use any global settings or registry.

Solution based on this Stack Overflow question: How to programmatic disable C# Console Application's Quick Edit mode?

$QuickEditCodeSnippet=@" 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

 
public static class DisableConsoleQuickEdit
{
 
const uint ENABLE_QUICK_EDIT = 0x0040;

// STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
const int STD_INPUT_HANDLE = -10;

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

public static bool SetQuickEdit(bool SetEnabled)
{

    IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);

    // get current console mode
    uint consoleMode;
    if (!GetConsoleMode(consoleHandle, out consoleMode))
    {
        // ERROR: Unable to get console mode.
        return false;
    }

    // Clear the quick edit bit in the mode flags
    if (SetEnabled)
    {
        consoleMode &= ~ENABLE_QUICK_EDIT;
    }
    else
    {
        consoleMode |= ENABLE_QUICK_EDIT;
    }

    // set the new mode
    if (!SetConsoleMode(consoleHandle, consoleMode))
    {
        // ERROR: Unable to set console mode
        return false;
    }

    return true;
}
}

"@

$QuickEditMode=add-type -TypeDefinition $QuickEditCodeSnippet -Language CSharp


function Set-QuickEdit() 
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$false, HelpMessage="This switch will disable Console QuickEdit option")]
    [switch]$DisableQuickEdit=$false
)


    if([DisableConsoleQuickEdit]::SetQuickEdit($DisableQuickEdit))
    {
        Write-Output "QuickEdit settings has been updated."
    }
    else
    {
        Write-Output "Something went wrong."
    }
}

Now you can disable or enable Quick Edit option by:

Set-QuickEdit -DisableQuickEdit
Set-QuickEdit 
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
Krisz
  • 701
  • 7
  • 23
  • Thank you for this. Yes, it's too late - I'm long past the problem. But if I have time to test this out, I will do that and mark as answer. Looks very promising. OTOH, perhaps you could comment on the relevance of AveYo's comment below? – David I. McIntosh Sep 23 '17 at 22:34
  • I had the same problem and your answer worked well for me. Thanks! – Kevinoid Nov 30 '17 at 18:34
1

David,

I’m not sure if you’ve found the solution to your problem or not, but I came across your post while researching a way to have a PowerShell scrip disable the Quick Edit option under the Edit Options. As far as I know from my research, Rahul is correct: the only way to make this change ‘programmatically’ is through the registry. I know you said that you don’t want to change the registry, but there is a way to change the registry value, start a new powershell process, execute a script block in that powershell process, then change the registry value back. This is how you would do it:

Assuming the necessary registry value does not exist:

Set-Location HKCU:\Console
New-Item ‘.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe’
Set-Location ‘.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe’
New-ItemProperty . QuickEdit –Type DWORD –Value 0x00000000
Start-Process Powershell.exe “&{ <#The scrip block you want to run with Quick Edit disabled#> }”
Set-ItemProperty . QuickEdit –Value 0x00000001
Pop-Location

Assuming the necessary registry value does exist:

Set-Location HKCU:\Console
Set-Location ‘.%SystemRoot%_System32_WindowsPowerShell_v1.0_Powershell.exe’
Set-ItemProperty . QuickEdit –Value 0x00000000
Start-Process Powershell.exe “&{ <#The scrip block you want to run with Quick Edit disabled#> }”
Set-ItemProperty . QuickEdit –Value 0x00000001
Pop-Location

If you insert this code into the portion of your script that you want to run with Quick Edit disabled, you should get the result you’re looking for. Hopefully this helps.

-Cliff

  • Thanks for the reply. Yes, that will work, but there are other, simpler ways to solve the real problem I have. Disabling quick-edit would have solved my problem it it could be done simply, but since it can't be done simply, I can resort to other methods of solving my real problem. – David I. McIntosh Aug 07 '15 at 02:40
0

Krisz' and Clifford's answers didn't work for me. I had a powershell script that I wanted to run (a long-running script that should NOT be interrupted by mouse clicks). In my example, I use DoWork.ps1 to be that long-running script.

I created a batch file to set the reg-key values:

RunScript.bat

@echo off
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%DoWork.ps1

: Disable QuickEdit
PowerShell -Command "Set-ItemProperty -Path 'HKCU:\Console' -Name 'QuickEdit' -Value 0x00000000" 
PowerShell -Command "Set-ItemProperty -Path 'HKCU:\Console\%%SystemRoot%%_System32_WindowsPowerShell_v1.0_Powershell.exe' -Name 'QuickEdit' -Value 0x00000000" 

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}"

: Re-enable QuickEdit 
PowerShell -Command "Set-ItemProperty -Path 'HKCU:\Console' -Name 'QuickEdit' -Value 0x00000001" 
PowerShell -Command "Set-ItemProperty -Path 'HKCU:\Console\%%SystemRoot%%_System32_WindowsPowerShell_v1.0_Powershell.exe' -Name 'QuickEdit' -Value 0x00000001" 

DoWork.ps1

$i = 0
while ($i -lt 1000)
{
    Write-Output "i:$i"
    $i++
    sleep(1)
}

Put RunScript.bat and DoWork.ps1 in the same folder and double click/invoke RunScript.bat to kick off DoWork.ps1. Notice that the QuickEdit checkbox in the Powershell "Properties" setting is disabled.

Setting HKCU:\Console\QuickEdit to 0 disables QuickEdit "Default" setting only for the Command Prompt and setting HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe\QuickEdit to 0 disables QuickEdit "Properties" setting for the Powershell window that is invoked by the RunScript.bat file.

AFAIK, I don't think QuickEdit "Default" setting affects anything. But what seems to be important is that:

  1. HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe\QuickEdit must be set to 0.
  2. The powershell script you wish to run MUST be invoked from a batch file. Even if you have both registry keys set to 0, the QuickEdit "Properties" checkbox will still checked (enabled).
MTV
  • 91
  • 9