Plenty of people have asked, and gotten several variations of answers to, the question "How do I get the path of the script itself in Powershell?". However, in my situation I have a few utility functions stored in a common module alongside the script but I don't actually run the script from that particular directory, instead I have symlink'd the script to $HOME\bin, which I have in PATH. And I do not want to symlink all the utility libraries into the $HOME\bin directory.
How can I get the path of the "real" script path in Powershell, given that the script the user actually runs (i.e. is found in PATH) can be a symlink?
It's a little clunky, but use the common $PSCommandPath to get the script pathname, then try to look up what it links to. If there's no result then $PSCommandPath is the answer. Otherwise check if it's an absolute link target path; if it is then that is the answer. Otherwise join the path of the symlink with its target. Finally Resolve-Path is used to "remove" the relative part of the merged pathname.
Function Get-RealScriptPath() {
# Get script path and name
$ScriptPath = $PSCommandPath
# Attempt to extract link target from script pathname
$link_target = Get-Item $ScriptPath | Select-Object -ExpandProperty Target
# If it's not a link ..
If(-Not($link_target)) {
# .. then the script path is the answer.
return $ScriptPath
}
# If the link target is absolute ..
$is_absolute = [System.IO.Path]::IsPathRooted($link_target)
if($is_absolute) {
# .. then it is the answer.
return $link_target
}
# At this point:
# - we know that script was launched from a link
# - the link target is probably relative (depending on how accurate
# IsPathRooted() is).
# Try to make an absolute path by merging the script directory and the link
# target and then normalize it through Resolve-Path.
$joined = Join-Path $PSScriptRoot $link_target
$resolved = Resolve-Path -Path $joined
return $resolved
}
Function Get-ScriptDirectory() {
$ScriptPath = Get-RealScriptPath
$ScriptDir = Split-Path -Parent $ScriptPath
return $ScriptDir
}
$ScriptDir = Get-ScriptDirectory
So, I can find out the directory of the script with
function Get-ScriptDirectory { Split-Path -parent $PSCommandPath }
But I can't manage to change the directory to the folder, where the script is saved. For example
cd d:\MyScripts\
So something like
cd function Get-ScriptDirectory { Split-Path -parent $PSCommandPath }
doesn't work.
And sorry if that is a common and dumb question, but I'm new in powershell and I searched and tried a lot before posting here
You can do it like below. You can include the code sample in your script. Then if you are running the script from other location (by means of fully qualifying path name to the script); $MyInvocation will pickup the invocation location.
From Documentation
$MyInvocation
Contains an information about the current command, such as the name,
parameters, parameter values, and information about how the command was
started, called, or "invoked," such as the name of the script that called
the current command.
Code Sample
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
cd $dir
I have script located in C:\projects\bacon\packages\build\run.ps1 and I am trying to locate the solution folder (in this case bacon). Everything I've found shows how to climb forward if you know the folder name. But I don't know the project name, so I need to climb backwards until I find the first containing folder that has a packages or dependencies folder within the given script's path.
The closest function I've found is to use Split-Path $script:MyInvocation.MyCommand.Path to get my script's path and perhaps loop backwards somehow? but I can't find anyway of looping the folders backwards until I find the "packages" or "dependencies" folder.
You can use a combination of Get-Item and Get-ChildItem. Get-Item returns an object that has a Parent property. You can limit Get-ChildItem to just directory objects. You can then use this to trek backwards:
$current = Get-Item .
Write-Host $Current.Parent
do
{
$parent = Get-Item $current.Parent.FullName
$childDirectories = $parent | Get-ChildItem -Directory | ? { $_.Name -in #("dependencies","packages") }
$current = $parent
} until ($childDirectories)
$bacon = $parent.FullName
I should say that the first line $current = Get-Item . will only work as is if the current path for the PowerShell runspace is at the end of the tree you are working with.
In your script, if you are using v3, you can replace the . with $PSScriptRoot.
This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance, when I execute C:\temp\myscripts\mycmdlet.ps1 which has a settings file at C:\temp\myscripts\settings.xml I would like to be able to store C:\temp\myscripts in a variable within mycmdlet.ps1.
This is one solution which works (although a bit cumbersome):
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$settingspath = $directorypath + '\settings.xml'
Another one suggested this solution which only works on our test environment:
$settingspath = '.\settings.xml'
I like the latter approach a lot and prefer it to having to parse the filepath as a parameter each time, but I can't get it to work on my development environment. What should I do? Does it have something to do with how PowerShell is configured?
Yes, that should work. But if you need to see the absolute path, this is all you need:
(Get-Item .).FullName
The reliable way to do this is just like you showed $MyInvocation.MyCommand.Path.
Using relative paths will be based on $pwd, in PowerShell, the current directory for an application, or the current working directory for a .NET API.
PowerShell v3+:
Use the automatic variable $PSScriptRoot.
The easiest method seems to be to use the following predefined variable:
$PSScriptRoot
about_Automatic_Variables and about_Scripts both state:
In PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in PowerShell 3.0, it is valid in all scripts.
I use it like this:
$MyFileName = "data.txt"
$filebase = Join-Path $PSScriptRoot $MyFileName
You can also use:
(Resolve-Path .\).Path
The part in brackets returns a PathInfo object.
(Available since PowerShell 2.0.)
Try :
(Get-Location).path
or:
($pwd).path
Path is often null. This function is safer.
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value;
if($Invocation.PSScriptRoot)
{
$Invocation.PSScriptRoot;
}
Elseif($Invocation.MyCommand.Path)
{
Split-Path $Invocation.MyCommand.Path
}
else
{
$Invocation.InvocationName.Substring(0,$Invocation.InvocationName.LastIndexOf("\"));
}
}
Get-Location will return the current location:
$Currentlocation = Get-Location
I like the one-line solution :)
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Try this:
$WorkingDir = Convert-Path .
In Powershell 3 and above you can simply use
$PSScriptRoot
If you just need the name of the current directory, you could do something like this:
((Get-Location) | Get-Item).Name
Assuming you are working from C:\Temp\Location\MyWorkingDirectory>
Output
MyWorkingDirectory
Most answers don't work when debugging in the following IDEs:
PS-ISE (PowerShell ISE)
VS Code (Visual Studio Code)
Because in those the $PSScriptRoot is empty and Resolve-Path .\ (and similars) will result in incorrect paths.
Freakydinde's answer is the only one that resolves those situations, so I up-voted that, but I don't think the Set-Location in that answer is really what is desired. So I fixed that and made the code a little clearer:
$directorypath = if ($PSScriptRoot) { $PSScriptRoot } `
elseif ($psise) { split-path $psise.CurrentFile.FullPath } `
elseif ($psEditor) { split-path $psEditor.GetEditorContext().CurrentFile.Path }
For what it's worth, to be a single-line solution, the below is a working solution for me.
$currFolderName = (Get-Location).Path.Substring((Get-Location).Path.LastIndexOf("\")+1)
The 1 at the end is to ignore the /.
Thanks to the posts above using the Get-Location cmdlet.
this function will set the prompt location to script path, dealing with the differents way to get scriptpath between vscode, psise and pwd :
function Set-CurrentLocation
{
$currentPath = $PSScriptRoot # AzureDevOps, Powershell
if (!$currentPath) { $currentPath = Split-Path $pseditor.GetEditorContext().CurrentFile.Path -ErrorAction SilentlyContinue } # VSCode
if (!$currentPath) { $currentPath = Split-Path $psISE.CurrentFile.FullPath -ErrorAction SilentlyContinue } # PsISE
if ($currentPath) { Set-Location $currentPath }
}
You would think that using '.\' as the path means that it's the invocation path. But not all the time. Example, if you use it inside a job ScriptBlock. In which case, it might point to %profile%\Documents.
This is what I came up with. It's an array including multiple methods of finding a path, uses the current location, filters out null\empty results, and returns the first not-null value.
#((
($MyInvocation.MyCommand.Module.ModuleBase),
($PSScriptRoot),
(Split-Path -Parent -Path $MyInvocation.MyCommand.Definition -ErrorAction SilentlyContinue),
(Get-Location | Select-Object -ExpandProperty Path)
) | Where-Object { $_ })[0]
To only get the current folder name, you can also use:
(Split-Path -Path (Get-Location) -Leaf)
To expand on #Cradle 's answer: you could also write a multi-purpose function that will get you the same result per the OP's question:
Function Get-AbsolutePath {
[CmdletBinding()]
Param(
[parameter(
Mandatory=$false,
ValueFromPipeline=$true
)]
[String]$relativePath=".\"
)
if (Test-Path -Path $relativePath) {
return (Get-Item -Path $relativePath).FullName -replace "\\$", ""
} else {
Write-Error -Message "'$relativePath' is not a valid path" -ErrorId 1 -ErrorAction Stop
}
}
I had similar problems and it made me a lot of trouble since I am making programs written in PowerShell (full end user GUI applications) and I have a lot of files and resources I need to load from disk.
From my experience, using . to represent current directory is unreliable. It should represent current working directory, but it often does not.
It appears that PowerShell saves location from which PowerShell has been invoked inside ..
To be more precise, when PowerShell is first started, it starts, by default, inside your home user directory. That is usually directory of your user account, something like C:\USERS\YOUR USER NAME.
After that, PowerShell changes directory to either directory from which you invoked it, or to directory where script you are executing is located before either presenting you with PowerShell prompt or running the script. But that happens after PowerShell app itself originally starts inside your home user directory.
And . represents that initial directory inside which PowerShell started. So . only represents current directory in case if you invoked PowerShell from the wanted directory. If you later change directory in PowerShell code, change appears not to be reflected inside . in every case.
In some cases . represents current working directory, and in others directory from which PowerShell (itself, not the script) has been invoked, what can lead to inconsistent results.
For this reason I use invoker script. PowerShell script with single command inside:
POWERSHELL.
That will ensure that PowerShell is invoked from the wanted directory and thus make . represent current directory. But it only works if you do not change directory later in PowerShell code.
In case of a script, I use invoker script which is similar to last one I mentioned, except it contains a file option:
POWERSHELL -FILE DRIVE:\PATH\SCRIPT NAME.PS1.
That ensures that PowerShell is started inside current working directory.
Simply clicking on script invokes PowerShell from your home user directory no matter where script is located.
It results with current working directory being directory where script is located, but PowerShell invocation directory being C:\USERS\YOUR USER NAME, and with . returning one of these two directories depending on the situation, what is ridiculous.
But to avoid all this fuss and using invoker script, you can simply use either $PWD or $PSSCRIPTROOT instead of . to represent current directory depending on weather you wish to represent current working directory or directory from which script has been invoked.
And if you, for some reason, want to retrieve other of two directories which . returns, you can use $HOME.
I personally just have invoker script inside root directory of my apps I develop with PowerShell which invokes my main app script, and simply remember to never ever change current working directory inside my source code of my app, so I never have to worry about this, and I can use . to represent current directory and to support relative file addressing in my applications without any problems.
This should work in newer versions of PowerShell (newer than version 2).
Mine was a short, so unplug everything but USB from it and recompile
I was asked to write a PowerShell script that they can package in with their build updates. They will complete a build that gets dropped to a folder (say \server\build\release1.1.2). We need a script that takes all the files/folders from that folder and copies them to the appropriately named locations.
I need the script to also read the # of the current build from the folder title and create that same # build folder when it copies. Easy enough to that however I need the references to be all dynamic, so when that Release1.1.3 comes out wecan drop the same script into there and it will copy all the files to the appropriate directories (and create them if they don't exist).
This script should get you started. Run it to see an example of the values it produces.
# variable name chosen based on the automatic variable available to PowerShell Modules
$PSScriptRoot = ($MyInvocation.MyCommand.Path | Split-Path | Resolve-Path).ProviderPath
$BuildName = $PSScriptRoot | Split-Path -Leaf
#"
This script file is located at:
$($MyInvocation.MyCommand.Path)
The folder this script file is in is:
$PSScriptRoot
The name of the folder this script file is in is:
$BuildName
To copy files you might do this:
Copy-Item -Path `$PSScriptRoot\* -Destination C:\Install\`$BuildName -Recurse
"#