I'm using a power shell script to copy some files from my computer to a USB drive. However, even though I'm catching the System.IO exception, I still get the error at the bottom. How do I properly catch this exception, so it shows the message in my Catch block.
CLS
$parentDirectory="C:\Users\someUser"
$userDirectory="someUserDirectory"
$copyDrive="E:"
$folderName="Downloads"
$date = Get-Date
$dateDay=$date.Day
$dateMonth=$date.Month
$dateYear=$date.Year
$folderDate=$dateDay.ToString()+"-"+$dateMonth.ToString()+"-"+$dateYear.ToString();
Try{
New-Item -Path $copyDrive\$folderDate -ItemType directory
Copy-Item $parentDirectory\$userDirectory\$folderName\* $copyDrive\$folderDate
}
Catch [System.IO]
{
WriteOutput "Directory Exists Already"
}
New-Item : Item with specified name E:\16-12-2014 already exists.
At C:\Users\someUser\Desktop\checkexist.ps1:15 char:9
+ New-Item -Path $copyDrive\$folderDate -ItemType directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (E:\16-12-2014:String) [New-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand