Forum Discussion
copying files to an existing folder
Hello Keke_000
Welcome to the Microsoft community, my name is Recep I'll be happy to help you today.
It seems like you want to check if a folder exists, and if it does, copy the contents of another folder into it. If it doesn't exist, you create the folder and then copy the contents. Also, you've mentioned that you want to overwrite the existing folder if it's already there.
Here's an updated version of your script:
Start-Transcript
# Define the source and destination paths
$sourcePath = "C:\Charts"
$destinationPath = "$env:APPDATA\Microsoft\Templates\Charts"
# Check if the folder exists
if (Test-Path -Path $destinationPath) {
Write-Host "Folder already exists." -ForegroundColor Yellow
# Remove existing destination folder and its content
Remove-Item -Path $destinationPath -Recurse -Force
}
# Create a new folder
New-Item -Path $destinationPath -ItemType Directory -Force
Write-Host "Folder created successfully." -ForegroundColor Green
# Copy files from source to destination
Copy-Item -Path "$sourcePath\*" -Destination $destinationPath -Recurse -Force
Stop-Transcript
If I have answered your question, please mark your post as Solved If you like my response, please give it a Like Appreciate your Kudos! Proud to contribute! 🙂 |