Reputation: 315
I have a script that performs a copy-item from one Windows share to another that occasionally shows an error of 'Cannot access the file because it is being used by another process'. The code that performs this is as follows and already contains an -ErrorAction SilentlyContinue but still the errors appear on the console:
Get-ChildItem $sourceDir -Filter $itemNum*.* | ForEach {
Copy-Item -LiteralPath $sourceDir\$_ -Destination $destDir -ErrorAction SilentlyContinue
}
I have additional code that handles when an item cannot be copied which works perfectly, I just don't want the red error message to show on the console. As '-errorAction SilentlyContinue' doesn't seems to work, is there an alternative way?
PS. The file type that always causes issue is .msg files. Any reason why these would be problematic? I always assume it's because they have embedded attachments and files etc and are likely being scanned by on access AV as a result of the GCI on them?
Powershell is version 3.0 running on Windows Server 2008.
Upvotes: 0
Views: 1203
Reputation: 4678
This happens in quite a lot of commands, it's really annoying. To fix it you can try using a try/catch as in this basic example:
Get-ChildItem $sourceDir -Filter $itemNum*.* | ForEach {
try {
Copy-Item -LiteralPath $sourceDir\$_ -Destination $destDir -ErrorAction Stop
}
catch {}
}
Upvotes: 2