erik121
erik121

Reputation: 341

copy files from a folder to a new folder

I am making a script to create a folder and copy all the text files from a specified folder on the local machine to the new folder. I have the create a folder but I am not sure how to copy all the text files from a folder to the new one I created

$destDir = Read-Host -Prompt 'Please enter the new folder name: '

# Check if the folder exist if not create it 

$dir = $destDir
if(!(Test-Path -Path $dir )){
    New-Item -ItemType directory -Path $dir
    Write-Host "New folder created"
}
else
{
  Write-Host "Folder already exists"
}
# Check if the folder exist if not create it 

If (!(Test-Path $destDir)) {

   md $dir

}

          }

Upvotes: 1

Views: 2885

Answers (1)

Moshe perez
Moshe perez

Reputation: 1726

This will do:

Copy-Item -path $sourceDir\*.txt -Destination $destDir

Upvotes: 1

Related Questions