Powershell, Mass moving files of a certain type Powershell, Mass moving files of a certain type powershell powershell

Powershell, Mass moving files of a certain type


A few things:

  1. You can pass the text you write to the screen directly to the read-host cmdlet, it saves you a line per user input.

  2. As a rule of thumb, if you plan to do more with an output of a command, DO NOT pipe it to the format-* cmdlets. The format cmdlets produces formatting objects that instructs powershell how to display the result on screen.

  3. Try to avoid assigning the result to a variable, if the result contains a large set of file system, memory consumption can go very high and you can suffer a performance degradation.

  4. Again, in terms of performance, try to use cmdlet parameters instead of the where-object cmdlet (server side filtering vs. client side). The first filters the objects on the target while the latter filters the objects only after the get to your machine.

The WhatIf switch will show you which files would have moved. Remove it to execute the command. You may also need to twick it to deal with duplicate file names.

$sourceDir = read-host "Please enter source Dir"$format = read-host "Format to look for"Get-ChildItem -Path $sourceDir -Filter $format -Recurse | Move-Item -Destination $sourceDir -Whatif


If I understood your question correctly, you can use Move-Item on files to move them to the output directory:

$Dir = get-childitem $sourceDir -recurse$files = $Dir | where {$_.extension -eq "$format"}$files | move-item -destination $outDir


A previous poster pointed out that the script would overwrite files of the same name. It might be possible to extend the script by a test and to avoid that possibility. Something like this:

$sourceDir = read-host "Please enter source Dir"$format = read-host "Format to look for?"$destDir = read-host "Please enter Destination Dir"Get-ChildItem -Path $sourceDir -Filter $format -Recurse | Copy-Item   -Destination $DestDir $files = $DestDir | where {$_.extension -eq "$format"} If (Test-Path $files) {    $i = 0    While (Test-Path $DestinationFile) {        $i += 1        $DestinationFile = "$files$i.$format"        Copy-Item -Destination $DestDir  $DestinationFile    }} Else {    $DestinationFile = "$files$i.$format"    Copy-Item -Destination $DestDir $DestinationFile}Copy-Item -Path $SourceFile -Destination $DestinationFile -Force