【发布时间】:2010-11-14 01:37:21
【问题描述】:
如何在 PowerShell 中执行此操作。在批处理文件中,我会这样做: %~d0%~p0
【问题讨论】:
-
顺便说一下,你可以把它缩短到 %~dp0
标签: powershell
如何在 PowerShell 中执行此操作。在批处理文件中,我会这样做: %~d0%~p0
【问题讨论】:
标签: powershell
来自Get-ScriptDirectory to the Rescue博客条目...
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
【讨论】:
Split-Path $MyInvocation.MyCommand.Path -Parent
【讨论】:
$MyInvocation 是什么?
$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.
对于PowerShell 3.0 用户 - 以下适用于模块和脚本文件:
function Get-ScriptDirectory {
Split-Path -parent $PSCommandPath
}
【讨论】:
在 powershell 2.0 中
分割路径$pwd
【讨论】: