【问题标题】:PowerShell configuration with an rc-like file带有类 rc 文件的 PowerShell 配置
【发布时间】:2012-08-22 00:22:37
【问题描述】:

为了定义我应该写什么脚本和哪里:

  1. alias for ll="ls -l"
  2. alias/function cd = "original cd; ll"

所以,我的问题是 Windows 7 上 Power Shell 的 rc 文件在哪里以及如何将 ll 别名为 ls -lcd 别名为 cd; ll

【问题讨论】:

    标签: powershell windows-7 scripting


    【解决方案1】:

    在键入$profile 时在power shell 指向的位置创建一个文件,如果该文件不存在,请按Enter。 (更多信息请查看here。)

    我还发现了很多很好的例子,在我的系统中powershell.exe旁边有一个示例文件夹,其中有一个名为profile.ps1的文件,代码如下:

    set-alias cat        get-content
    set-alias cd         set-location
    set-alias clear      clear-host
    set-alias cp         copy-item
    set-alias h          get-history
    set-alias history    get-history
    set-alias kill       stop-process
    set-alias lp         out-printer
    set-alias ls         get-childitem
    set-alias mount      new-mshdrive
    set-alias mv         move-item
    set-alias popd       pop-location
    set-alias ps         get-process
    set-alias pushd      push-location
    set-alias pwd        get-location
    set-alias r          invoke-history
    set-alias rm         remove-item
    set-alias rmdir      remove-item
    set-alias echo       write-output
    
    set-alias cls        clear-host
    set-alias chdir      set-location
    set-alias copy       copy-item
    set-alias del        remove-item
    set-alias dir        get-childitem
    set-alias erase      remove-item
    set-alias move       move-item
    set-alias rd         remove-item
    set-alias ren        rename-item
    set-alias set        set-variable
    set-alias type       get-content
    
    function help
    {
        get-help $args[0] | out-host -paging
    }
    
    function man
    {
        get-help $args[0] | out-host -paging
    }
    
    function mkdir
    {
        new-item -type directory -path $args
    }
    
    function md
    {
        new-item -type directory -path $args
    }
    
    function prompt
    {
        "PS " + $(get-location) + "> "
    }
    
    & {
        for ($i = 0; $i -lt 26; $i++) 
        { 
            $funcname = ([System.Char]($i+65)) + ':'
            $str = "function global:$funcname { set-location $funcname } " 
            invoke-expression $str 
        }
    }
    

    还要考虑以下问题。在执行位于$profile 的文件时,您可能会遇到以下错误:

    “Microsoft.PowerShell_profile.ps”无法加载,因为此系统上禁用了脚本的执行。有关详细信息,请参阅“get-help about_signing”。

    解决方案: 检查当前的执行策略

    PS C:\Windows\System32> Get-ExecutionPolicy
    Restricted
    PS C:\Windows\System32>
    

    要更改执行策略以允许 PowerShell 从本地文件执行脚本,请运行以下命令:

    PS C:\Windows\System32> Set-Executionpolicy RemoteSigned -Scope CurrentUser
    

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 2021-06-22
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 2015-07-30
      • 2018-11-19
      • 2019-06-26
      • 2023-03-30
      相关资源
      最近更新 更多