【问题标题】:Path of currently executing powershell script [duplicate]当前执行powershell脚本的路径[重复]
【发布时间】:2010-11-14 01:37:21
【问题描述】:

如何在 PowerShell 中执行此操作。在批处理文件中,我会这样做: %~d0%~p0

【问题讨论】:

标签: powershell


【解决方案1】:

来自Get-ScriptDirectory to the Rescue博客条目...

function Get-ScriptDirectory
{
  $Invocation = (Get-Variable MyInvocation -Scope 1).Value
  Split-Path $Invocation.MyCommand.Path
}

【讨论】:

【解决方案2】:
Split-Path $MyInvocation.MyCommand.Path -Parent

【讨论】:

  • $MyInvocation 是什么?
  • @alex $MyInvocation 是 PowerShell 创建的自动变量。来自文档(链接):Contains information about the current command, such as the name, parameters, parameter values, and information about how the command was started, called, or invoked, such as the name of the script that called the current command.
【解决方案3】:

对于PowerShell 3.0 用户 - 以下适用于模块和脚本文件:

function Get-ScriptDirectory {
    Split-Path -parent $PSCommandPath
}

【讨论】:

    【解决方案4】:

    在 powershell 2.0 中

    分割路径$pwd

    【讨论】:

    • Wroking 目录与 scripts 目录不同。
    猜你喜欢
    • 1970-01-01
    • 2010-10-01
    • 2010-10-12
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多