windows用powershell调亮度

有时夜晚将亮度调到最暗,早上想调回来时太暗又看不到,想写一个脚本调。参考 [1,2],可用 powershell 命令实现。顺便记录 powershell 脚本参数写法。

Code

# brightness.ps1

# 命令行参数 `b`,[0, 100],默认 50
param ($b = 50)
# echo $b
# 改亮度
(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,$b)

powershell 脚本后缀是 .ps1,存为 brightness.ps1。接受命令行参数的写法参考 [3],设默认值参考 [4]。运行:

.\brightness.ps1
# 或指定亮度 [0, 100]
.\brightness.ps1 -b 90

不过 powershell 默认禁止运行脚本,参考 [5,6],可以:

  • 提前在任一 powershell 运行 Set-ExecutionPolicy -Scope CurrentUser Bypass对当前用户)赋权。会记在注册表,今后都能运行,可用 Get-ExecutionPolicy -Scope CurrentUser 验证;
  • 或用 unblock-file brightness.ps1[7,8];(无效)
  • 或在当前 powershell(对此 shell)赋权:Set-ExecutionPolicy -Scope Process Bypass;(有点麻烦)
  • 或用 powershell -ExecutionPolicy Bypass 另起一个赋权的子 shell。

Run in cmd

powershell 脚本只能在 powershell 入面直接用 .\<script>.ps1<path>\<script>.ps1 执行,且要放 bypass 权才方便。

如果不想放权,参考 [9],考虑写个 .bat 文件在 cmd 套娃运行 powershell 脚本,用到 powershell 命令。

@REM bright.bat
@echo off
@REM 命令行参数
if (%1) == () (
	set b=50
) else (
	set b=%1
)
@REM echo %b%
@REM 执行 powershell 脚本,临时放权
powershell -executionpolicy bypass -File brightness.ps1 %b%

执行 bright.batbright.bat <亮度> 即可,放在 %USERPROFILE%/ 下(cmd 启动路径)即可 Ctrl + R 呼出 cmd 后盲敲。

References

  1. 7 ways to adjust the screen brightness in Windows 10
  2. Windows 11 Change Display Brightness with 9 methods
  3. about_Scripts
  4. about_Parameters_Default_Values
  5. about_Execution_Policies
  6. Set-ExecutionPolicy
  7. about_Signing
  8. Unblock-File
  9. How to run a PowerShell script
  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值