Powershell中的Powershell文件的文件夹路径

Kar*_*son 25 powershell

似乎没有人问这个简单的问题,所以我会.

我的PowerShell脚本文件位于: C:/this-folder/that-folder/another-folder/powershell-file.ps1

如何获得返回的变量: C:/this-folder/that-folder/another-folder/

CB.*_*CB. 34

在脚本中尝试此命令:

Split-Path -parent $MyInvocation.MyCommand.Definition
Run Code Online (Sandbox Code Playgroud)


Sha*_*evy 17

在PowerShell 3.0中,您可以使用新$PSScriptRoot变量获取它,并且$PSCommandPath您可以获得完整的脚本路径.

MVP Keith山也有一篇很棒的文章你可能想检查:

http://rkeithhill.wordpress.com/2010/09/19/determining-scriptdir-safely/


MiF*_*vil 12

您可以使用标准 .NET 方法:

$dirName = [System.IO.Path]::GetDirectoryName("c:\temp\abc\myproj1\newdata.txt")
Run Code Online (Sandbox Code Playgroud)

PowerShell:从文件或目录路径获取父目录名称