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/
?
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.
In that case, it is located at C:\Windows\SysWOW64\WindowsPowerShell\v1.
Try this command in your script:
Split-Path -parent $MyInvocation.MyCommand.Definition
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With