How to Recursively Move Files Multiple Times With Powershell?

10 minutes read

To recursively move files multiple times with PowerShell, you can use the Get-ChildItem cmdlet to retrieve a list of files in a directory, then iterate through each file and move it to the desired location using the Move-Item cmdlet. You can also use the -Recurse parameter with Get-ChildItem to include all files in subdirectories.


Here's a basic example:

1
2
3
4
5
6
7
8
9
# Specify the source and destination directories
$sourceDir = "C:\Source"
$destinationDir = "C:\Destination"

# Retrieve all files in the source directory and its subdirectories
Get-ChildItem $sourceDir -Recurse | ForEach-Object {
    # Move each file to the destination directory
    Move-Item $_.FullName -Destination $destinationDir
}


This script will recursively move all files from the $sourceDir directory and its subdirectories to the $destinationDir directory. You can customize the paths and add additional logic as needed for your specific requirements.

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 while preserving the directory structure in PowerShell?

To move files while preserving the directory structure in PowerShell, you can use the following command:

1
Copy-Item -Path 'C:\source\*' -Destination 'C:\destination\' -Recurse


This command will recursively copy all files and directories from the source folder to the destination folder while preserving the directory structure. If you want to move the files instead of copying them, you can use the Move-Item command in place of Copy-Item.


Here's an example using Move-Item:

1
Move-Item -Path 'C:\source\*' -Destination 'C:\destination\' -Recurse


Remember to replace C:\source\ and C:\destination\ with the actual paths to your source and destination folders.


How to move files based on their creation date in PowerShell?

To move files based on their creation date in PowerShell, you can use the Get-ChildItem cmdlet to get a list of files and then filter them based on their creation date using the CreationTime property. You can then use the Move-Item cmdlet to move the files to a specific directory.


Here is an example PowerShell script that moves files based on their creation date:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Set the source and destination directory paths
$sourceDirectory = "C:\path\to\source\directory"
$destinationDirectory = "C:\path\to\destination\directory"

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

foreach ($file in $files) {
    # Check if the file creation date is older than a specific date
    if ($file.CreationTime -lt (Get-Date).AddDays(-30)) {
        # Move the file to the destination directory
        Move-Item -Path $file.FullName -Destination $destinationDirectory
        Write-Host "Moved $($file.Name) to $destinationDirectory"
    }
}


In this script, we are moving files that were created more than 30 days ago from the source directory to the destination directory. You can customize the script to fit your specific requirements, such as changing the date comparison or destination directory.


How to rename files while moving them in PowerShell recursively?

You can use the Move-Item cmdlet along with the Get-ChildItem cmdlet to recursively move and rename files in PowerShell. Here's an example of how you can do this:

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

Get-ChildItem -Path $sourceDirectory -Recurse | ForEach-Object {
    $newFileName = $_.Name.Replace("oldstring", "newstring")
    Move-Item -Path $_.FullName -Destination "$destinationDirectory\$newFileName"
}


In this script:

  1. Replace $sourceDirectory with the path to the source directory from where you want to move the files.
  2. Replace $destinationDirectory with the path to the destination directory to where you want to move the files.
  3. The Get-ChildItem cmdlet retrieves all files and folders recursively from the specified source directory.
  4. The ForEach-Object loop iterates over each file and folder.
  5. Inside the loop, $_.Name gets the current file or folder name, and the Replace method replaces the old string with the new string in the file name.
  6. The Move-Item cmdlet moves the file to the specified destination directory with the new file name.


By running this script, files will be moved from the source directory to the destination directory, and each file will be renamed with the specified replacement string.


How to move files older than a certain date in PowerShell?

To move files older than a certain date in PowerShell, you can use the following script:

1
2
3
4
5
$sourceFolder = "C:\SourceFolder"
$destinationFolder = "C:\DestinationFolder"
$dateLimit = (Get-Date).AddDays(-30)

Get-ChildItem $sourceFolder | Where-Object { $_.LastWriteTime -lt $dateLimit } | Move-Item -Destination $destinationFolder


In this script:

  1. Replace "C:\SourceFolder" with the path of the folder containing the files you want to move.
  2. Replace "C:\DestinationFolder" with the path of the folder where you want to move the files to.
  3. Set the $dateLimit variable to the cutoff date. In the provided example, it is set to move files older than 30 days.
  4. The Get-ChildItem cmdlet retrieves the files in the source folder.
  5. The Where-Object cmdlet filters the files based on the LastWriteTime property.
  6. The Move-Item cmdlet moves the filtered files to the destination folder.


Run the script in PowerShell to move files older than the specified date to the destination folder.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

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 move files based on their birth time in Hadoop, you can use the Hadoop File System (HDFS) command hadoop fs -mv in conjunction with the -t flag to specify the target directory. First, you can use the hadoop fs -ls command to list the files in the source dir...
To get the previous files based on the missing files using PowerShell, you can compare the list of missing files with the list of all files in a directory. You can use the Get-ChildItem cmdlet to list all files in the directory and then compare it with the lis...