Folder Path of Powershell file In Powershell

前端 未结 4 1837
别跟我提以往
别跟我提以往 2021-02-05 02:38

No one seems to ask this simple question, so I will.

My PowerShell script file is located: C:/this-folder/that-folder/another-folder/powershell-file.ps1

相关标签:
4条回答
  • 2021-02-05 02:59

    In PowerShell 3.0 you can get it with the new $PSScriptRoot variable, and with $PSCommandPath you can get the full script path.

    There's also a great post by MVP Keith hill you may want to check:

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

    0 讨论(0)
  • 2021-02-05 02:59

    I'm assuming you want to know what folder your script is running in when it's being run.

    This should do it:

    Split-Path $MyInvocation.MyCommand.Path
    
    0 讨论(0)
  • 2021-02-05 03:08

    try this command in your script:

    Split-Path -parent $MyInvocation.MyCommand.Definition
    
    0 讨论(0)
  • 2021-02-05 03:20

    You can use standard .Net method

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

    From http://techibee.com/powershell/powershell-get-parent-directory-name-from-file-or-directory-path/2595

    0 讨论(0)
提交回复
热议问题