HOWTO · PowerShell

How to Move Files and Folders Using PowerShell

Learn how to check paths, create files and folders, and move them with PowerShell.

On this page

Use PowerShell’s Move-Item cmdlet to move a file or folder to its parent directory or another destination. For a reliable move, check paths with Test-Path, create a missing destination with New-Item, then move the item. These steps can also be adapted to move a selected set of files.

Check if a File or Folder Exists Using PowerShell

Test-Path returns $true when a file or directory exists at the supplied path, and $false otherwise. Replace the sample path with the file or folder you want to check. This self-contained example creates a sample file first, then checks for it:

$sampleDir = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString())
New-Item -ItemType Directory -Path $sampleDir | Out-Null
$sampleFile = Join-Path $sampleDir 'test.txt'
New-Item -ItemType File -Path $sampleFile | Out-Null
Test-Path -Path $sampleFile

Creating Files and Folders Using PowerShell

Use New-Item with -ItemType Directory to create a folder, or -ItemType File to create a file. -Path specifies the full path, including the new item’s name. This example creates both beneath a temporary directory:

$sampleDir = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString())
New-Item -ItemType Directory -Path $sampleDir | Out-Null
$folderPath = Join-Path $sampleDir 'PS'
New-Item -ItemType Directory -Path $folderPath | Out-Null
$filePath = Join-Path $folderPath 'TestFile.txt'
New-Item -ItemType File -Path $filePath | Out-Null
Test-Path -Path $filePath

Moving Files and Folders Using PowerShell

Move-Item moves a file or folder from the path supplied to -Path to the location supplied to -Destination. The destination can be .. for the parent directory or a different folder. It must already exist: Move-Item does not create a missing destination. The following self-contained example creates a file and folder, moves the file to its parent, creates a custom destination if needed, then moves the folder there. It prints the two path checks as Boolean values.

$root = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString())
$sourceDir = Join-Path $root 'Source'
New-Item -ItemType Directory -Path $sourceDir | Out-Null
$filePath = Join-Path $sourceDir 'TestFile.txt'
New-Item -ItemType File -Path $filePath | Out-Null
$folderPath = Join-Path $sourceDir 'PSScripts'
New-Item -ItemType Directory -Path $folderPath | Out-Null

Push-Location $sourceDir
Move-Item -Path 'TestFile.txt' -Destination '..'
Pop-Location
Test-Path -Path (Join-Path $root 'TestFile.txt')

$destination = Join-Path $root 'Archive'
if (-not (Test-Path -Path $destination)) {
    New-Item -ItemType Directory -Path $destination | Out-Null
}
Move-Item -Path $folderPath -Destination $destination
Test-Path -Path (Join-Path $destination 'PSScripts')