Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Folder Path of Powershell file In Powershell

Tags:

powershell

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

How do I get a variable that returns C:/this-folder/that-folder/another-folder/?

like image 347
basickarl Avatar asked Jan 30 '12 12:01

basickarl


People also ask

How do I find a folder in PowerShell?

On a Windows computer from PowerShell or cmd.exe, you can display a graphical view of a directory structure with the tree.com command. To get a list of directories, use the Directory parameter or the Attributes parameter with the Directory property. You can use the Recurse parameter with Directory.

What is the path of PowerShell?

In that case, it is located at C:\Windows\SysWOW64\WindowsPowerShell\v1.


Video Answer


3 Answers

Try this command in your script:

Split-Path -parent $MyInvocation.MyCommand.Definition
like image 71
CB. Avatar answered Oct 16 '22 17:10

CB.


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: Determining $ScriptDir Safely

like image 18
Shay Levy Avatar answered Oct 16 '22 18:10

Shay Levy


You can use a standard .NET method:

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

From PowerShell: Get parent directory name from file or directory path.

like image 6
Michael Freidgeim Avatar answered Oct 16 '22 18:10

Michael Freidgeim