Run script within push and pop location
See original GitHub issueSummary of the new feature / enhancement
As a user, I want to be able to execute code in a different directory so that I can quickly run a command and return to my current state.
Having a single command for this removes the need for wrapping these commands in pushd
and popd
I am unaware of this functionality existing elsewhere, although the only place I could think of was Invoke-Command
Proposed technical implementation details (optional)
This can be achieved with the following script:
function Use-Location {
[CmdletBinding()] param (
[Parameter(Mandatory)] [string] $Location,
[Parameter(Mandatory)] [scriptblock] $ScriptBlock
)
Push-Location -Path $Location
Invoke-Command -ScriptBlock $ScriptBlock
Pop-Location
}
Example usage:
"repo1/my-project", "repo2/my-other-project" | %{
Use-Location $_ {dotnet build}
}
Issue Analytics
- State:
- Created 3 months ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to use Push-Location and Pop-Location command in ...
First, it saves the current location to the top of the stack, and second, it browse the path specified. If there is no...
Read more >Push-Location (Microsoft.PowerShell.Management)
You can use the Pop-Location cmdlet to get locations from the location stack. ... This command pushes the current location onto the default...
Read more >How to automatically call Pop-Location at end of scope
Short answer: No. My take on Push-Location and Pop-Location is that you should generally avoid them, and adapt your script to use the...
Read more >Pop-Location (Microsoft.PowerShell.Management)
The Pop-Location cmdlet changes the current location to the location most recently pushed onto the stack by using the Push-Location cmdlet.
Read more >push/pop current directory? - linux
Excerpt from the link pushd Saves the current directory on the top of the directory stack and then cd to dir. With no...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This improvement could cover the risk of unpaired Push-Location calls in the invoked script block, mentioned by @fflaten above.
An alternative implementation.
Thank you for the feedback about the missing
Pop-Location
issue, and especially for the multiple suggested solutions, that is helpful.My request was prompted after writing a loop for building and publishing packages:
I thought it would be cleaner if I could do the following instead:
As this is intended to be committed in a repo, it would not work having this function only defined in my profile.