PowerShell Navigation and File Manipulation

How to navigate the filesystem and manage files and directories from the PowerShell terminal.

Navigation

pwd

Present working directory — tells you where you are in the file structure.

pwd

Sample Output:

PS C:\Users\ellma\> pwd

Path
----
C:\Users\ellma\

cd

Allows you to move between directories.

Starting at C:\Users\<your username>\ type:

cd Documents

Now run pwd to confirm:

Path
----
C:\Users\ellma\Documents

New-Item

Used to create new items — files, folders, registry keys, or other objects at a specified location. Item types you can create:

  • File — creates a file
  • Directory — creates a folder
  • SymbolicLink — creates a symbolic link (shortcut)
  • HardLink — creates a hard link
  • Junction — creates a directory junction (like a symlink, but for directories)
New-Item -Path <path> -Name <name> -ItemType <type>

Creating a new file:

New-Item -Path C:\Users\ellma\Documents

Output:

    Directory: C:\Users\ellma\Documents

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         9/22/2024   3:17 PM              0 firstfile.txt

Creating Directories

You have several options for creating a new directory.

Use New-Item:

New-Item Subdir -ItemType Directory

Use md (note the syntax change):

md "C:\Users\ellma\Documents\subdir2"

The Linux mkdir command is also available:

mkdir "C:\Users\ellma\Documents\subdir3"

Listing Items in Directories

PowerShell's native command is Get-ChildItem, but ls and dir are both set as aliases — all three produce the same output.

Get-ChildItem -Path C:\Users\ellma\Documents
ls "C:\Users\ellma\Documents"
dir -Path "C:\Users\YourUsername\Documents" -Filter *.txt

Output:

    Directory: C:\Users\ellma\Documents

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         9/22/2024   3:26 PM                Subdir
-a----         9/22/2024   3:29 PM              0 firstfile.txt

Copying Items

Basic copy syntax:

copy sourceFile.txt destinationFolder

Example using the cp alias:

cp "C:\Users\ellma\Documents\exampledoc" "C:\Users\ellma\Documents\secondfile"

The Linux cp command is available but doesn't offer all the features you may need, especially when handling many files or directories.

Alternative Commands

xcopy — more powerful for copying directories, subdirectories, and files including hidden ones:

xcopy sourceDir destinationDir /s /e
  • /s — copies directories and subdirectories, except empty ones
  • /e — copies directories and subdirectories, including empty ones

robocopy — recommended for copying large numbers of files or directories:

robocopy sourceDir destinationDir /mir

Removing Items

Starting state:

PS C:\Users\ellma\Documents> ls

    Directory: C:\Users\ellma\Documents

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         9/22/2024   3:26 PM                Subdir
d-----         9/22/2024   4:00 PM                subdir2
d-----         9/22/2024   4:00 PM                subdir3
-a----         9/22/2024   3:17 PM              0 exampledoc
-a----         9/22/2024   3:29 PM              0 secondfile.txt

Removing a File

Remove-Item -Path "C:\path\to\file.txt"

Example:

Remove-Item -Path "C:\Users\ellma\Documents\exampledoc"

Removing a Directory

Remove an empty directory:

Remove-Item -Path "C:\path\to\directory"

Example:

Remove-Item -Path "C:\Users\ellma\Documents\subdir3"

Remove a directory and all its contents:

Remove-Item -Path "C:\path\to\directory" -Recurse