似乎没有人问这个简单的问题,所以我会。Powershell文件的文件夹路径在Powershell
我的PowerShell脚本文件位于:C:/this-folder/that-folder/another-folder/powershell-file.ps1
我如何返回一个变量:C:/this-folder/that-folder/another-folder/
似乎没有人问这个简单的问题,所以我会。Powershell文件的文件夹路径在Powershell
我的PowerShell脚本文件位于:C:/this-folder/that-folder/another-folder/powershell-file.ps1
我如何返回一个变量:C:/this-folder/that-folder/another-folder/
在你的脚本试试这个命令:
Split-Path -parent $MyInvocation.MyCommand.Definition
我假设你想知道您的脚本在运行时正在运行什么文件夹。
这应做到:
Split-Path $MyInvocation.MyCommand.Path
这包括脚本名称,这是他已经知道,所以其他答案更正确。 – 2016-08-10 11:59:53
在PowerShell中3.0,你可以用新$PSScriptRoot
变量得到它,并与$PSCommandPath
你可以得到完整的脚本路径。
也有一个伟大的职位由MVP基思·希尔,你可能要检查:
http://rkeithhill.wordpress.com/2010/09/19/determining-scriptdir-safely/
您可以使用标准的.NET方法
$dirName=[System.IO.Path]::GetDirectoryName("c:\temp\abc\myproj1\newdata.txt")
从http://techibee.com/powershell/powershell-get-parent-directory-name-from-file-or-directory-path/2595
你是我的英雄,谢谢。 – 2012-01-30 15:10:01