2016-11-07 33 views
0

是位於C路線我有一個腳本(script1.ps1):\ FolderA如何找到位置/當前腳本在PowerShell中

我可以在腳本插入什麼PowerShell代碼,這將打印什麼都腳本的當前位置/路徑。 (我們的目標是讓這個腳本便攜式)

即:

在folderA - 當,代碼將打印C:\ FolderA(完整路徑)

- 當Script1.ps1移到C:\ FolderB \ FolderC並運行,它打印C:\ FolderB \ FolderC的完整路徑

任何幫助將不勝感激。

感謝


感謝@Nkosi和@Kori吉爾

我一直在尋找的答案是$ PSScriptRoot

有一點要注意的是,我發現我不得不定義在腳本運行腳本之前首先在腳本內部創建一個變量:

$Path = $PSScriptRoot 

$Path 

wi這工作,否則它沒有

+2

的可能的複製[?什麼是確定當前的PowerShell腳本的位置最好的辦法] (http://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script) – Nkosi

回答

3

請參閱「get-help about_Automatic_Variables」。

閱讀部分上:

  • $ MyInvocation
  • $ PSScriptRoot
  • $ PSCommandPath