Move Item - PowerShell


Overview

The Move-Item cmdlet allows you to move files and folders within and between drives. It’s particularly useful for reorganizing, managing, and consolidating file systems.

Syntax

Move-Item [-Path] <String[]> [-Destination] <String> [-Force] [-Verbose] [-WhatIf] [-Confirm]

Options/Flags

  • -Path: Specifies the path to the item(s) you want to move. Supports wildcards (* and ?) for selecting multiple items.
  • -Destination: Specifies the destination path for the moved item(s). Can be a new folder or an existing one.
  • -Force: Overwrites any existing items at the destination without prompting for confirmation.
  • -Verbose: Outputs detailed information about the move operation.
  • -WhatIf: Simulates the move operation without actually performing it. Shows what would happen if the command were executed.
  • -Confirm: Prompts for confirmation before moving the item(s).

Examples

Move a single file:

Move-Item -Path "C:\Users\username\Documents\file.txt" -Destination "C:\NewFolder"

Move multiple files with wildcards:

Move-Item -Path "C:\Users\username\Documents\*.txt" -Destination "C:\TextFiles"

Move a folder and its contents:

Move-Item -Path "C:\Users\username\Documents\OldFolder" -Destination "C:\NewLocation"

Overwrite existing items:

Move-Item -Path "C:\Users\username\Documents\file.txt" -Destination "C:\NewFolder" -Force

Common Issues

  • Error: File exists: Ensure the destination folder doesn’t already contain an item with the same name. Use the -Force option to overwrite it.
  • Error: Path not found: Verify that the specified path and destination exist and are accessible.
  • Error: Access denied: Check folder permissions to ensure you have sufficient access to both the source and destination folders.

Integration

Move-Item can be combined with other commands to automate file management tasks:

  • Get-ChildItem to list files and folders before moving.
  • New-Item to create a new folder for the moved items.
  • Remove-Item to delete any empty folders left behind after moving.
  • Copy-Item: Copies items without removing them from the original location.
  • Rename-Item: Renames files and folders.