PUSHD - CMD


Overview

The PUSHD command in Windows Command Prompt is used to save the current directory by pushing it onto a stack and then changing to a specified directory. This command is particularly useful in scripting and automation, where there might be a need to change the working directory temporarily and then return to the original directory.

Syntax

The basic syntax for the PUSHD command is as follows:

PUSHD [path]
  • path: Specifies the directory to change to. If the path is a network resource, PUSHD will create a temporary drive letter that points to that specified network resource and then change the current drive and directory.

Options/Flags

PUSHD does not have multiple flags or options, but its behavior with network paths acts as an implicit feature:

  • Network Paths: If the specified path is a network resource, PUSHD will assign a temporary drive letter that starts at the last available drive letter and works backward, which simplifies accessing shared resources during a Command Prompt session.

Examples

  1. Changing Directories Locally:

    PUSHD C:\Windows
    

    This command will save the current directory and then change to C:\Windows.

  2. Accessing Network Resources:

    PUSHD \\Server\Share
    

    This example pushes the current directory onto the stack and maps the network share \\Server\Share to a temporary drive letter, switching to that drive.

  3. Nested Directory Change:

    PUSHD C:\Users
    PUSHD Public\Documents
    POPD
    

    Move to C:\Users, dive deeper into Public\Documents, and then use POPD to go back to C:\Users.

Common Issues

  • Network Drive Mapping Limit:
    If all drive letters are in use, PUSHD cannot create another drive for a network resource. A workaround is to manually free up a drive letter.

  • Path Not Found:
    Ensure the path exists, especially with network paths, as typos or network issues can cause errors.

Integration

Combine PUSHD and POPD for efficient directory management in scripts:

@echo off
PUSHD %TEMP%
REM Perform operations in the temp directory
POPD
REM Back to original directory

This script temporarily switches to the Windows temp directory, performs tasks, and then returns to the original directory.

  • POPD: Returns to the directory stored by the last PUSHD.
  • CD (or CHDIR): Changes the directory.
  • DIR: Lists the contents of a directory.

For more details, you can utilize the built-in help available in CMD by executing help pushd or pushd /?, which provides brief built-in documentation.