Reputation: 40444
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/
?
Upvotes: 46
Views: 54413
Reputation: 28425
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.
Upvotes: 14
Reputation: 126702
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
Upvotes: 18
Reputation: 60910
Try this command in your script:
Split-Path -parent $MyInvocation.MyCommand.Definition
Upvotes: 59
Reputation: 52567
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
Upvotes: 2