Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 479 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the current directory of the cmdlet being executed

#11
If you just need the name of the current directory, you could do something like this:
```powershell
((Get-Location) | Get-Item).Name
```
Assuming you are working from C:\Temp\Location\MyWorkingDirectory>

**Output**

MyWorkingDirectory
Reply

#12
I like [the one-line solution][1] :)

$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

[1]:

[To see links please register here]

Reply

#13
[`Get-Location`][1] will return the current location:

$Currentlocation = Get-Location

[1]:

[To see links please register here]

Reply

#14
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][1] cmdlet.


[1]:

[To see links please register here]

Reply

#15
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).
Reply

#16
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 }
}
Reply

#17
Yes, that should work. But if you need to see the absolute path, this is all you need:

(Get-Item .).FullName

Reply

#18
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][1] 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:

<!-- language: lang-powershell -->

$directorypath = if ($PSScriptRoot) { $PSScriptRoot } `
elseif ($psise) { split-path $psise.CurrentFile.FullPath } `
elseif ($psEditor) { split-path $psEditor.GetEditorContext().CurrentFile.Path }

<!-- end snippet -->

[1]:

[To see links please register here]

Reply

#19
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.

```lang-php
@((
($MyInvocation.MyCommand.Module.ModuleBase),
($PSScriptRoot),
(Split-Path -Parent -Path $MyInvocation.MyCommand.Definition -ErrorAction SilentlyContinue),
(Get-Location | Select-Object -ExpandProperty Path)
) | Where-Object { $_ })[0]
```
Reply

#20
To only get the current folder name, you can also use:

(Split-Path -Path (Get-Location) -Leaf)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through