In Windows PowerShell (and CMD and bash) it would be nice to pushd and then copy to the last directory I was in. ex:

> pwd
D:\Some insanely long\path I really\ don/'t want to type\because it's hard\vimstuff\
> pushd ..\..\..\..\thing that\lives in the swamp
> cp *.pu $popd

where $popd would be the last directory pushed. Does this functionality exist or would I need to write a script?

Edit: Looks like everyone is answering helpful tips for getting closer to the solution but not quite there yet. It may not be possible in powershell. I was looking for something like the following that I wrote for cmd but doesn't work in powershell:


echo off
if "%olddirp%"=="" (
  echo olddirp not defined, use cdp to push directory before using cpp
) else (
  for %%A in ("" "help" "-help" "/help" "-h" "/h") do (
    if "%1"==%%A goto help
copy %1 %olddirp%
echo .\%1 copied to %olddirp%\%1
goto end
echo "cdp / cpp usage: cdp to directory 'cpp c:\newdir' then cpp files to previous directory 'cpp somefile'"


set olddirp=%cd%
cd %1

Can those be easily translated? I had trouble because apparently there's no %cd% or %path% or any other simple variable in powershell.


Try this, should do what your old bat file did. Save as yourName.ps1, and make sure you enable running of PowerShell-scripts, by starting powershell as admin and running "Set-ExecutionPolicy RemoteSigned".

Push a folder to stack with "pushd" then call this script with files/filepattern as arguments

script.ps1 *.pu,*.txt
Copies pu files and txt files to last folder pushed to stack.

  [Parameter(Mandatory=$False, Position=0)]


if($help -or !$files)
    (Get-Help $MyInvocation.MyCommand.Definition -full)

$CopyToFolder = $null;

    $CopyToFolder = (get-location -stack).peek();
    write-host "Stack is empty, use pushd before $($MyInvocation.MyCommand)";

foreach($f in $files)
    (copy $f $CopyToFolder);
    write-host ".\$files copied to $CopyToFolder\$files";
| improve this answer | |

In powershell, when you use push-location (i.e. pushd) it stores the location on a stack that you can later retrieve with get-location -Stack. So your example looks like this:

> pushd ..\..\..\..\thing that\lives in the swamp
> cp *.pu (get-location -stack)
| improve this answer | |
  • There could be multiple locations on the stack, it which case you would have to use cp *.pu (get-location -stack).peek() – Rynant Feb 24 '14 at 18:15
  • How would one make these into batch files like I've posted above in the question edit? What's the powershell equivalent of batch files and variables...? I can't get something simple like %cd% to work even. – Still.Tony Feb 24 '14 at 20:32

You could try to store the path into a variable (in my example, I assume I want to use the path from where the script was executed) and then use it as needed.

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

Set-Location -Path "C:\windows\system32"

Write-Host "Your current location: $(Get-Location)"

Write-Host "Your previous location: $popd"

Set-Location -Path $popd

Write-Host "We're back to: $(Get-Location)"

First, we store the path from where the script was called to the $popd variable. We then change directory to c:\windows\system32 and display that on-screen, as well as the original path (which was stored in $popd) and then use that variable to change back to the starting folder.

You can learn more about Automatic Variables such as $MyInvocation from this TechNet article.

Also, Andy Arismendi has provided an answer which addresses how to access the stack in PowerShell.

| improve this answer | |

Aide from the obvious:

cd "D:\Some insanely long\path I really\don't want to type again…"
copy "..\..\..\..\thing that\lives in the swamp\*.pu" .

You could also use subst (in Windows):

cd "D:\Some insanely long\path I really\don't want to type again…"
subst Z: .
cd "..\..\..\..\thing that\lives in the swamp"
copy *.pu Z:\
subst Z: /d
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.