1. 设置PowerShell脚本默认打开方式为powershell.exe
PowerShell脚本的后缀为.ps1(数字的1),默认打开方式是notepad.exe
修改默认打开方式为powershell.exe
ftype Microsoft.Powershellscript.1="%SystemRoot%\system32\windowspowershell\v1.0\powershell.exe" "%1"
2. PowerShell允许执行脚本
PowerShell默认执行策略是Restricted,禁止执行脚本
powershell.exe -Command ( Get-ExecutionPolicy )
type .\hello.ps1
powershell.exe -File .\hello.ps1
- 设置会话的执行策略,仅当会话有效
powershell.exe -Command ( Get-ExecutionPolicy )
powershell.exe -ExecutionPolicy Unrestricted -File .\hello.ps1
powershell.exe -Command ( Get-ExecutionPolicy )
- 修改系统级别的执行策略
powershell.exe -Command ( Get-ExecutionPolicy )
powershell.exe -Command ( Set-ExecutionPolicy -Force Unrestricted )
powershell.exe -Command ( Get-ExecutionPolicy )
powershell.exe -File .\hello.ps1
3. 执行PowerShell脚本的方法(已经设置允许执行PowerShell脚本)
资源浏览器
右键 -> Run with PowerShellcmd或powershell
powershell.exe -File .\hello.ps1powershell
.\hello.ps1cmd (已经设置.ps1文件默认打开方式为powershell.exe)
.\hello.ps1