[ PowerShell 提示出于兼容性目的已禁用PSReadLine问题 ]


但是,发现运行Import-Module PSReadLine后并没有解决。而办公室电脑的win10上很正常,并没有发现这个问题。

话说,是怎么发现这个问题的呢?倒也不是用PowerShell的时候,是因为用VS Code,发现终端界面上命令都没有高亮了,而终端用的就是PowerShell,感觉就是PowerShell的问题了。

于是,网上查了很多方法,下载了最新版的PowerShell也没用。

最终找到一个有用的。

解决方法:

1.创建一个.ps1文件(名字随便取,比如xiufu_powershell.ps1),粘贴以下代码:

Add-Type -TypeDefinition '
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public static class ScreenReaderFixUtil
{
    public static bool IsScreenReaderActive()
    {
        var ptr = IntPtr.Zero;
        try
        {
            ptr = Marshal.AllocHGlobal(sizeof(int));
            int hr = Interop.SystemParametersInfo(
                Interop.SPI_GETSCREENREADER,
                sizeof(int),
                ptr,
                0);
            if (hr == 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            return Marshal.ReadInt32(ptr) != 0;
        }
        finally
        {
            if (ptr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(ptr);
            }
        }
    }
    public static void SetScreenReaderActiveStatus(bool isActive)
    {
        int hr = Interop.SystemParametersInfo(
            Interop.SPI_SETSCREENREADER,
            isActive ? 1u : 0u,
            IntPtr.Zero,
            Interop.SPIF_SENDCHANGE);
        if (hr == 0)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    }
    private static class Interop
    {
        public const int SPIF_SENDCHANGE = 0x0002;
        public const int SPI_GETSCREENREADER = 0x0046;
        public const int SPI_SETSCREENREADER = 0x0047;
        [DllImport("user32", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int SystemParametersInfo(
            uint uiAction,
            uint uiParam,
            IntPtr pvParam,
            uint fWinIni);
    }
}'
if ([ScreenReaderFixUtil]::IsScreenReaderActive()) {
    [ScreenReaderFixUtil]::SetScreenReaderActiveStatus($false)
}

2.在PowerShell中运行这个文件

.\xiufu_powershell.ps1