I have a powershell script that archives all files that aren't ps1 and csv files, and it works, but if the zip archive already exists, I have to copy the archive or delete it before running the command again, otherwise the Error appears indicating that the file already exists.
Get-ChildItem -Path .\* -Exclude *.ps1, *.csv | Compress-Archive -DestinationPath .\NonEssArch.zip -verbose
Add-Type -AssemblyName PresentationFramework
$msgBoxInput = [System.Windows.MessageBox]::Show('Do you also want to delete these non-essential files?','Attention','YesNo','Info')
switch ($msgBoxInput) {
'Yes' {
del -Path *.* -Exclude *.csv, *.ps1, *.zip
}
'No' {
Exit
}
}
I want to be able to run the script after the archive has already been created, without having it replacing the existing archive. The problem lies in the "-DestinationPath .\NonEssArch.zip" part, but I am having trouble implementing a numbering system of sort.
Any help will be appreciated, thanks.
$NewFileName = 'NonEssarch_{0}.zip' -f (Get-Date).ToString('yyyy-MM-dd_HH-mm-ss')<<< otta do the job.