Script to move multiple folders base on a text file

rated by 0 users
Not Answered This post has 0 verified answers | 2 Replies | 2 Followers

Not Ranked
2 Posts
mike8332 posted on 04-10-2012 3:59 PM

I just started looking into PS, so my knowledge on PS is close to none.

I need a PS script to 
1. look into a text file on C:\ that contains folder names.
2. use that text file and see if some of those folders are in location A (D:\Folder1\folder2)
3. move those folders that exists in the text file and location A to location B (D:\Folder1)

Thank you in advance.

All Replies

Top 150 Contributor
34 Posts

Hi there

What about something like this?

$text = gc C:\foldernames.txt
$dir = Get-ChildItem D:\Folder1\folder2

foreach ($item in $text) {
  foreach ($folder in $dir) {
    if ($ -like $item) {
      Move-Item "D:\Folder1\folder2\$item" 'D:\Folder1'
      Write-Host "Folder $item moved successfully"
      [array]$totalitems += $item

"Folders moved: $totalitems"


You will also have a variable $totalitems with a collection of the folders that were successfully moved.

Hope it helps.


Not Ranked
2 Posts

Thank you I'll give it a try.

Page 1 of 1 (3 items) | RSS
Copyright 2012 All rights reserved.