powershell -- 筆記

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

  1. 設置會話的執行策略,僅當會話有效
    powershell.exe -Command ( Get-ExecutionPolicy )
    powershell.exe -ExecutionPolicy Unrestricted -File .\hello.ps1
    powershell.exe -Command ( Get-ExecutionPolicy )
  1. 修改系統級別的執行策略
    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腳本)
  1. 資源瀏覽器
    右鍵 -> Run with PowerShell

  2. cmd或powershell
    powershell.exe -File .\hello.ps1

  3. powershell
    .\hello.ps1

  4. cmd (已經設置.ps1文件默認打開方式為powershell.exe)
    .\hello.ps1

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容