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.
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
Based on @Krisz's answer i made an improved version to support both of Quick edit mode and Insert mode
$QuickEditCodeSnippet=@"
using System;
using System.Runtime.InteropServices;
public static class ConsoleModeSettings
{
const uint ENABLE_QUICK_EDIT = 0x0040;
const uint ENABLE_INSERT_MODE = 0x0020;
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 void EnableQuickEditMode()
{
SetConsoleFlag(ENABLE_QUICK_EDIT, true);
}
public static void DisableQuickEditMode()
{
SetConsoleFlag(ENABLE_QUICK_EDIT, false);
}
public static void EnableInsertMode()
{
SetConsoleFlag(ENABLE_INSERT_MODE, true);
}
public static void DisableInsertMode()
{
SetConsoleFlag(ENABLE_INSERT_MODE, false);
}
private static void SetConsoleFlag(uint modeFlag, bool enable)
{
IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
uint consoleMode;
if (GetConsoleMode(consoleHandle, out consoleMode))
{
if (enable)
consoleMode |= modeFlag;
else
consoleMode &= ~modeFlag;
SetConsoleMode(consoleHandle, consoleMode);
}
}
}
"@
$QuickEditMode=add-type -TypeDefinition $QuickEditCodeSnippet -Language CSharp
function Set-ConsoleProperties()
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[switch]$EnableQuickEditMode=$false,
[Parameter(Mandatory=$false)]
[switch]$DisableQuickEditMode=$false,
[Parameter(Mandatory=$false)]
[switch]$EnableInsertMode=$false,
[Parameter(Mandatory=$false)]
[switch]$DisableInsertMode=$false
)
if ($PSBoundParameters.Count -eq 0)
{
[ConsoleModeSettings]::EnableQuickEditMode()
[ConsoleModeSettings]::EnableInsertMode()
Write-Output "All settings have been enabled"
return
}
if ($EnableQuickEditMode)
{
[ConsoleModeSettings]::EnableQuickEditMode()
Write-Output "QuickEditMode has been enabled"
}
if ($DisableQuickEditMode)
{
[ConsoleModeSettings]::DisableQuickEditMode()
Write-Output "QuickEditMode has been disabled"
}
if ($EnableInsertMode)
{
[ConsoleModeSettings]::EnableInsertMode()
Write-Output "InsertMode has been enabled"
}
if ($DisableInsertMode)
{
[ConsoleModeSettings]::DisableInsertMode()
Write-Output "InsertMode has been disabled"
}
}
Usage:
Set-ConsoleProperties -EnableQuickEditMode -DisableInsertMode
Set-ConsoleProperties -DisableQuickEditMode -DisableInsertMode
Set-ConsoleProperties
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With