我的 PowerShell 脚本文件位于 C:/this-folder/that-folder/another-folder/powershell-file.ps1
。
如何获取一个变量,它返回 C:/this-folder/that-folder/another-folder/
?
我的 PowerShell 脚本文件位于 C:/this-folder/that-folder/another-folder/powershell-file.ps1
。
如何获取一个变量,它返回 C:/this-folder/that-folder/another-folder/
?
在您的脚本中尝试这个命令:
Split-Path -parent $MyInvocation.MyCommand.Definition
$PSScriptRoot
变量获取它,而使用$PSCommandPath
可获取完整的脚本路径。
此外,MVP Keith Hill还有一篇很棒的文章供您参考:安全确定$ScriptDir。您可以使用标准的.NET方法:
$dirName = [System.IO.Path]::GetDirectoryName("c:\temp\abc\myproj1\newdata.txt")
$dirName = "c:\temp\abc\myproj1"
即可。 - Lance U. MatthewsSplit-Path $MyInvocation.MyCommand.Path
powershell-file.ps1
如何获取其自身的父目录路径,但最好明确说明一下。 - Lance U. Matthews