How to Move Files From Specific Folders Only Using Powershell?

9 minutes read

To move files from specific folders using PowerShell, you can use the Move-Item cmdlet along with the -Path parameter to specify the source folder and the -Destination parameter to specify the destination folder. You can also use the -Recurse parameter to include subdirectories.


For example, to move all files from a specific folder named "Source" to another folder named "Destination", you can use the following command:

1
Move-Item -Path "C:\Path\To\Source\*" -Destination "C:\Path\To\Destination" -Force


This command will move all files (denoted by *) from the "Source" folder to the "Destination" folder. The -Force parameter is used to overwrite any existing files with the same name in the destination folder.


You can modify the command to suit your specific requirements, such as moving files based on their extension, file name, or any other criteria. Just replace the paths and parameters as needed.

Best PowerShell Books to Read in December 2024

1
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Rating is 5 out of 5

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

2
PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

Rating is 4.9 out of 5

PowerShell Cookbook: Your Complete Guide to Scripting the Ubiquitous Object-Based Shell

3
Scripting: Automation with Bash, PowerShell, and Python

Rating is 4.8 out of 5

Scripting: Automation with Bash, PowerShell, and Python

4
Learn PowerShell Scripting in a Month of Lunches

Rating is 4.7 out of 5

Learn PowerShell Scripting in a Month of Lunches

5
Mastering PowerShell Scripting - Fourth Edition: Automate and manage your environment using PowerShell 7.1

Rating is 4.6 out of 5

Mastering PowerShell Scripting - Fourth Edition: Automate and manage your environment using PowerShell 7.1

6
Practical Automation with PowerShell: Effective scripting from the console to the cloud

Rating is 4.5 out of 5

Practical Automation with PowerShell: Effective scripting from the console to the cloud

7
Mastering PowerShell Scripting - Fifth Edition: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

Rating is 4.4 out of 5

Mastering PowerShell Scripting - Fifth Edition: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

8
PowerShell for Sysadmins: Workflow Automation Made Easy

Rating is 4.3 out of 5

PowerShell for Sysadmins: Workflow Automation Made Easy

  • Book - powershell for sysadmins: workflow automation made easy
9
PowerShell Pocket Reference: Portable Help for PowerShell Scripters

Rating is 4.2 out of 5

PowerShell Pocket Reference: Portable Help for PowerShell Scripters


How to move files from a single folder using PowerShell?

To move files from a single folder using PowerShell, you can use the Move-Item cmdlet.


Here's an example command that moves all files from one folder to another:

1
Move-Item -Path "C:\sourcefolder\*" -Destination "C:\destinationfolder\" 


In this command:

  • -Path "C:\sourcefolder\*" specifies the path to the source folder with a wildcard character (*) to include all files in the folder.
  • -Destination "C:\destinationfolder\" specifies the destination folder where the files will be moved.


You can also filter which files to move based on their extension or other criteria by modifying the -Path parameter accordingly.


How to move files based on their file name using PowerShell?

To move files based on their file name using PowerShell, you can use the Get-ChildItem cmdlet to get a list of files and then use a Where-Object cmdlet to filter the files based on their file name. You can then use the Move-Item cmdlet to move the files to a new location.


Here's an example of how you can move files based on their file name using PowerShell:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Set the source and destination directories
$sourceDir = "C:\Source"
$destinationDir = "C:\Destination"

# Get a list of files in the source directory
$files = Get-ChildItem $sourceDir

# Filter the files based on their file name
$filesToMove = $files | Where-Object { $_.Name -like "*pattern*" }

# Move the files to the destination directory
$filesToMove | ForEach-Object {
    Move-Item $_.FullName -Destination $destinationDir
}


In this example, replace C:\Source with the path of the source directory where the files are located and C:\Destination with the path of the destination directory where you want to move the files. Replace *pattern* with the file name pattern you want to search for.


This script will move all files with the specified file name pattern from the source directory to the destination directory.


How to move files based on their last access date using PowerShell?

You can use the following PowerShell script to move files based on their last access date:

1
2
3
4
5
6
7
$sourceDirectory = "C:\Source"
$destinationDirectory = "C:\Destination"
$daysThreshold = 30

Get-ChildItem $sourceDirectory | Where-Object {($_.LastAccessTime -lt (Get-Date).AddDays(-$daysThreshold))} | ForEach-Object {
    Move-Item $_.FullName -Destination $destinationDirectory
}


In this script:

  1. Set the $sourceDirectory variable to the directory containing the files you want to move.
  2. Set the $destinationDirectory variable to the directory where you want to move the files.
  3. Set the $daysThreshold variable to the number of days since the last access date that you want to use as a threshold for moving the files.
  4. The script uses the Get-ChildItem cmdlet to retrieve a list of files in the source directory.
  5. The Where-Object cmdlet filters the files based on their last access time using the $daysThreshold variable.
  6. The ForEach-Object cmdlet iterates over the filtered files and uses the Move-Item cmdlet to move each file to the destination directory.


Run the script in PowerShell to move the files based on their last access date. Adjust the source directory, destination directory, and days threshold as needed for your specific requirements.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To compare two folders and add/remove files with PowerShell, you can use the Compare-Object cmdlet. This cmdlet compares two sets of objects and indicates differences. You can use it to compare files in two folders by specifying the folders as the input object...
To open a PowerShell console window from an existing PowerShell session, you can use the Start-Process cmdlet with the -FilePath parameter to specify the path to the PowerShell executable (powershell.exe).Here is the command you can use: Start-Process powershe...
To check who has access to folders using Command Prompt (cmd) or Powershell, you can use the "icacls" command. Open Command Prompt or Powershell and navigate to the folder you want to check the access permissions for. Then, type the command "icacls...