PowerShell: Copy all folders with a specific name from .txt file?

$applications = get-content c:\applications.txt
foreach-object {Get-ChildItem "\\sdlcorpwwps01\WiseSharePoint\Projects" -Recurse | Where-Object {($_.PSIsContainer) -AND $_.NAME -eq $applications} | Move-Item -Destination "\\sdlcorpwwps01\smspkgsource$\Archives\Projects"}

Every time I run this command it wants to copy all the folders. I will be using this script to automate our archiving process. Any help?

Thanks! :D

1 Comment   [ + ] Show comment
  • Hi,
    I'm just starting my PowerShell adventure so my suggestion is basing on my VBS knowledge. I think that there might be a problem with $applications array and its usage in -eq operator. Usually comparison is done between two items so based on VBS syntax I would try with additional foreach to enumerate $applications list:

    $applications = get-content "c:\applications.txt"
    foreach ($App in $applications){
    foreach-object {Get-ChildItem "\\sdlcorpwwps01\WiseSharePoint\Projects" -Recurse | Where-Object {($_.PSIsContainer) -AND $_.NAME -eq $App} | Move-Item -Destination "\\sdlcorpwwps01\smspkgsource$\Archives\Projects"}
    } - rad33k 6 years ago
    • This worked for one of the folders but I need all of then in my .txt file to be moved.

      https://drive.google.com/file/d/0BzYq22atosQPS09QUG9kdVJELWs/edit?usp=sharing - mlubbers 6 years ago
      • Are there any errors in the PowerShell console?
        I've tested it with local paths and worked fine for all your folders.
        Maybe the problem is with Move-item cmdlet and network paths:
        My another thought is maybe you didn't wait for the completion of the job - as there are over hundred of entries in TXT file and probably much more directories in your REPO it probably will take awhile to finish. - rad33k 6 years ago

Answers (1)

Posted by: refLye 6 years ago
White Belt
Here is a suggestion:

$baseDir = "E:\test"
$targetDir = "E:\test\target"
$toCopyPath = "E:\test\tocopy.txt"

$toCopy = Get-Content $toCopyPath

$toCopy | ForEach-Object {
  Copy-Item "$baseDir\$_" $targetDir -Recurse -Force

Content of tocopy.txt


The folder hierarchy:


│   │   copy.ps1
│   │   tocopy.txt
│   │
│   ├───foldera
│   ├───folderb
│   ├───folderc
│   └───target
│       ├───folderb
│       └───folderc

The yellow marked folders are created after copy.ps1 has started.

This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ