开机自启动Powershell脚本

目录

前言

这绝B是个非常受用的技能。

修改注册表

Open Registry Editor, add a startup item
i. Locate the path“HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionRun”
Create a string value name Shadow
这里写图片描述
ii. Right click Shadow and choose Modify…
这里写图片描述
iii. Add the path of the cmd file above. For example: C:UsersuserNameDesktopShadow.cmd, and OK.
这里写图片描述

写批处理

Open a notepad and paste the command below (This file will invoke PowerShell script)
执行当前目录下同名的.ps1脚本。

@set Path=%Path%;%SystemRoot%system32WindowsPowerShellv1.0 & powershell -ExecutionPolicy Unrestricted -NoProfile %~dpn0.ps1
exit

Save as shadow.cmd on your desktop.
这里写图片描述
注意:并不需要照搬上面的内容,你可以在.cmd这个文件中自定义希望被调用的Powershell脚本,就可以实现开机自启动Powershell脚本了。后面的内容作为实验记录,仅供参考。

以管理员方式打开Posershell程序

Open a notepad and paste the command below (This file will open PowerShell with administrator permission)

Start-Process "$PSHOMEpowershell.exe" -Verb runas

Save as shadow.ps1 on your desktop.
这里写图片描述

修改PS-profile

Create a profile for current user’s PowerShell (This file will import module when PowerShell start)

$a= (Get-Host).UI.RawUI
$a.WindowTitle="MCShadow"
$a.ForegroundColor="Green"
$b=$a.WindowSize
$b.Width="120"
$b.Height="57"
$a.WindowSize=$b
Import-Module D:ShadowMod.psm1
Start-Shadow
Write-Host "Imported Module ShadowMod.psm1"
Write-Host "Ready to use Start or Stop Shadow”

Save as Profile.ps1 under this path “C:UsersuserNameDocumentsWindowsPowerShell”, if you can’t find this folder, please create it by manual.

最后

后部分的内容是对PS-profile的实验内容,自启动的原理还是在于注册表的修改和.cmd文件对指定PSScript的调用。 :-)

原文地址:https://www.cnblogs.com/jmilkfan-fanguiju/p/7533731.html