Script commands to disable quick edit mode

前端 未结 2 1162
难免孤独
难免孤独 2020-12-21 12:58

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 "Qu

相关标签:
2条回答
  • 2020-12-21 13:12

    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

    0 讨论(0)
  • 2020-12-21 13:13

    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 
    
    0 讨论(0)
提交回复
热议问题