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