似乎沒有人問這個簡單的問題,所以我會。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