Reputation: 341
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
Reputation: 1726
This will do:
Copy-Item -path $sourceDir\*.txt -Destination $destDir
Upvotes: 1