3

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
Path
----
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:

CPP.BAT:

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
:help
echo "cdp / cpp usage: cdp to directory 'cpp c:\newdir' then cpp files to previous directory 'cpp somefile'"
:end

CDP.BAT:

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.

4 Answers 4

1

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".

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

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

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

   [alias("h")]
   [switch]$help
)

if($help -or !$files)
{
    (Get-Help $MyInvocation.MyCommand.Definition -full)
    exit(0);
}

$CopyToFolder = $null;

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


foreach($f in $files)
{
    (copy $f $CopyToFolder);
    write-host ".\$files copied to $CopyToFolder\$files";
}
3

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)
2
  • 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, 2014 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, 2014 at 20:32
1

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.

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .