AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1493334
Accepted
A__
A__
Asked: 2019-10-18 07:34:40 +0800 CST2019-10-18 07:34:40 +0800 CST 2019-10-18 07:34:40 +0800 CST

如何在 Win10 Pro 上通过 PowerShell 设置显示分辨率

  • 772

客户的 IT 管理员要求我“编写一个在启动时手动设置分辨率的 powershell 脚本”。如果他们要求我这样做,大概是有可能的。我对powershell完全没有经验。我的脚本/命令及其错误如下:

Set-DisplayResolution -Width 1024 -Height 768

Set-DisplayResolution不被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

Set-ScreenResolution -Width 1024 -Height 768

Set-ScreenResolution不被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

SetDisplayResolution -Width 1024 -Height 768

SetDisplayResolution不被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

我错过了什么?谢谢你。

在此处输入图像描述

powershell resolution
  • 4 4 个回答
  • 35118 Views

4 个回答

  • Voted
  1. Best Answer
    Albin
    2019-10-18T09:35:15+08:002019-10-18T09:35:15+08:00

    不知道你从哪里得到你的例子,但在本机 powershell 中没有设置分辨率的命令。

    我使用 AutoHotKey。您可以在那里构建自己的脚本或使用网上的示例。

    或者您可以编写自己的函数,可以从 powershell 调用:请参见此处

    当然,根据您的要求,还有一些 3rd 方工具。这是一个也适用于脚本的示例,或者这里有7 个其他示例。你到底想做什么?

    • 4
  2. A__
    2021-07-10T08:53:56+08:002021-07-10T08:53:56+08:00

    每阿尔宾的回答:

    使用以下内容 (src) 创建一个 setResolution.ps1 文件:

    Function Set-ScreenResolution { 
    
    <# 
        .Synopsis 
            Sets the Screen Resolution of the primary monitor 
        .Description 
            Uses Pinvoke and ChangeDisplaySettings Win32API to make the change 
        .Example 
            Set-ScreenResolution -Width 1024 -Height 768         
        #> 
    param ( 
    [Parameter(Mandatory=$true, 
               Position = 0)] 
    [int] 
    $Width, 
    
    [Parameter(Mandatory=$true, 
               Position = 1)] 
    [int] 
    $Height 
    ) 
    
    $pinvokeCode = @" 
    
    using System; 
    using System.Runtime.InteropServices; 
    
    namespace Resolution 
    { 
    
        [StructLayout(LayoutKind.Sequential)] 
        public struct DEVMODE1 
        { 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
            public string dmDeviceName; 
            public short dmSpecVersion; 
            public short dmDriverVersion; 
            public short dmSize; 
            public short dmDriverExtra; 
            public int dmFields; 
    
            public short dmOrientation; 
            public short dmPaperSize; 
            public short dmPaperLength; 
            public short dmPaperWidth; 
    
            public short dmScale; 
            public short dmCopies; 
            public short dmDefaultSource; 
            public short dmPrintQuality; 
            public short dmColor; 
            public short dmDuplex; 
            public short dmYResolution; 
            public short dmTTOption; 
            public short dmCollate; 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
            public string dmFormName; 
            public short dmLogPixels; 
            public short dmBitsPerPel; 
            public int dmPelsWidth; 
            public int dmPelsHeight; 
    
            public int dmDisplayFlags; 
            public int dmDisplayFrequency; 
    
            public int dmICMMethod; 
            public int dmICMIntent; 
            public int dmMediaType; 
            public int dmDitherType; 
            public int dmReserved1; 
            public int dmReserved2; 
    
            public int dmPanningWidth; 
            public int dmPanningHeight; 
        }; 
    
    
    
        class User_32 
        { 
            [DllImport("user32.dll")] 
            public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode); 
            [DllImport("user32.dll")] 
            public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags); 
    
            public const int ENUM_CURRENT_SETTINGS = -1; 
            public const int CDS_UPDATEREGISTRY = 0x01; 
            public const int CDS_TEST = 0x02; 
            public const int DISP_CHANGE_SUCCESSFUL = 0; 
            public const int DISP_CHANGE_RESTART = 1; 
            public const int DISP_CHANGE_FAILED = -1; 
        } 
    
    
    
        public class PrmaryScreenResolution 
        { 
            static public string ChangeResolution(int width, int height) 
            { 
    
                DEVMODE1 dm = GetDevMode1(); 
    
                if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm)) 
                { 
    
                    dm.dmPelsWidth = width; 
                    dm.dmPelsHeight = height; 
    
                    int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST); 
    
                    if (iRet == User_32.DISP_CHANGE_FAILED) 
                    { 
                        return "Unable To Process Your Request. Sorry For This Inconvenience."; 
                    } 
                    else 
                    { 
                        iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY); 
                        switch (iRet) 
                        { 
                            case User_32.DISP_CHANGE_SUCCESSFUL: 
                                { 
                                    return "Success"; 
                                } 
                            case User_32.DISP_CHANGE_RESTART: 
                                { 
                                    return "You Need To Reboot For The Change To Happen.\n If You Feel Any Problem After Rebooting Your Machine\nThen Try To Change Resolution In Safe Mode."; 
                                } 
                            default: 
                                { 
                                    return "Failed To Change The Resolution"; 
                                } 
                        } 
    
                    } 
    
    
                } 
                else 
                { 
                    return "Failed To Change The Resolution."; 
                } 
            } 
    
            private static DEVMODE1 GetDevMode1() 
            { 
                DEVMODE1 dm = new DEVMODE1(); 
                dm.dmDeviceName = new String(new char[32]); 
                dm.dmFormName = new String(new char[32]); 
                dm.dmSize = (short)Marshal.SizeOf(dm); 
                return dm; 
            } 
        } 
    } 
    
    "@ 
    
    Add-Type $pinvokeCode -ErrorAction SilentlyContinue 
    [Resolution.PrmaryScreenResolution]::ChangeResolution($width,$height) 
    } 
    
    Set-ScreenResolution -Width 1024 -Height 768
    

    然后可以从powershell执行文件如下

    `C:\path-to-file\setResolution.ps1`
    
    • 1
  3. Yechiel Worenklein
    2022-04-27T01:25:58+08:002022-04-27T01:25:58+08:00

    您需要从 Powershell-Gallery安装DisplaySettings模块才能获得此功能。

    复制并粘贴以下命令以使用 PowerShellGet 安装此包

    Install-Module -Name DisplaySettings
    
    • 1
  4. John Peachey
    2022-02-15T04:44:53+08:002022-02-15T04:44:53+08:00

    Albin 帖子中的 powershell 脚本运行良好 - 但在一台机器上只能让我设置 1920x1080 而不是 1920x1200,我可以手动设置。最后我不得不使用 Nirsoft MultiMonitor,它可以让你保存和加载配置文件。我能够编辑配置文件并输入分辨率参数,它工作得很好。配置文件如下所示:

    Name=\\.\DISPLAY1
    Width=1920
    Height=1200
    

    虽然我更愿意尽可能避免使用第三方工具,但在这种情况下,它是免费的、轻量级的,而且运行良好(并且没有抱怨任何病毒)......

    • 0

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    Windows 10 服务称为 AarSvc_70f961。它是什么,我该如何禁用它?

    • 2 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Marko Smith

    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)

    • 4 个回答
  • Marko Smith

    我如何知道 Windows 安装在哪个驱动器上?

    • 6 个回答
  • Martin Hope
    Albin 支持结束后如何激活 WindowsXP? 2019-11-18 03:50:17 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    Kagaratsch Windows 10 删除大量小文件的速度非常慢。有什么办法可以加快速度吗? 2019-09-23 06:05:43 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    Inter Sys Ctrl+C 和 Ctrl+V 是如何工作的? 2019-05-15 02:51:21 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve