Powershell Won't Release a file

I have copied an image file to a directory with powershell.

Copy-Item -Path c:\example.jpg -Destination d:\example.jpg Recurse -Force

I then run  xaml in powershell that uses the file as an image in a wps application.

<Image Name="example" HorizontalAlignment="Left" Height="188" Margin="257,444,0,0" VerticalAlignment="Top" Width="149" Source="D:\example.jpg" Cursor="Arrow"/>

The image can be viewed and the script works as intended up until this point.

Once the xaml has run I can no longer remove, rename or copy over the top of example.jpg until I close powershell. Even after I close the wps application I still cannot delete, rename or copy over the file either in powershell or in windows explorer until I close powershell.

This is a problem because my intention is to copy over the top of example.jpg with a different image later on in the script.

I have tried using handle.exe to confirm the file is only being used by powershell and to end the example.jpg handle without ending powershell but it did not work.

I have also tried using .close() and .dispose() after the wps has closed i.e.

$example = "D:\example.jpg"
$example.close()


$example = "D:\example.jpg"
$example.dispose()

I can still delete and/or edit example.jpg after I use copy-item but before I run the wps app so I know I need to somehow make powershell release the file once the wps app has ended.

Does anyone know a way I can force powershell to release this file without exiting the program altogether?

Any help would be greatly appreciated.

P.S.

Here is the error code I get when I try edit the file.

Remove-Item : Cannot remove item D:\example.jpg: The process cannot access the file 'D:\example.jpg' because it is being used by another process.
At line:1 char:53
+ Remove-Item D:\example.jpg -Recurse -force
+                                                     ~~~~~~~~~~~
    + CategoryInfo          : WriteError: (DD:\example.jpg:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand



August 31st, 2015 11:46am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics