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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容