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 3:44pm

You have to use your forms variable names and not  mine.

I also typed it wrong it should be "Remove-Variable".

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 4:05am

For the variable

Image Name="testpic" 
I have tried the following commands one at a time:

$form.Controls|%{$_.Dispose();remove-variable $_.testpic}
$form.Controls|%{$testpic.Dispose();remove-variable $_.testpic}
$form.Controls|%{$testpic.Dispose();remove-variable $testpic.name}
$form.Controls|%{$testpic.Dispose();remove-variable $testpic}
It comes up with this error:

You cannot call a method on a null-valued expression.
At line:1 char:18
+ $form.Controls|%{$testpic.Dispose();remove-variable $testpic.Name}
+                  ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Remove-Variable : Cannot bind argument to parameter 'Name' because it is null.
At line:1 char:54
+ $form.Controls|%{$testpic.Dispose();remove-variable $testpic.Name}
+                                                      ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Variable], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveVariableCommand


September 1st, 2015 6:40am

Hi Hedgehog

the objects are null.

$form.Controls|? {$_} | %{$_.Dispose();remove-variable $_.testpic}

Try this - actually not sure if it works.

Also - dont forget the $xaml and your $R

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 7:33am

Sorry but I don't have the time to teach you PowerShell.

Try this:

$testpic.Dispose()
Remove-Variable testpic.

And David...stop guessing.  Look at the loop.  There is no control with a property called "testpic".  Testpic is a name for a control.  The variable generator loop creates variables by enumerating the "Controls" collection and creating a variable based on the control name.  We need to release the image controls and remove all references to allow the image files to be closed.  This is a bug in the WPF classes.  It should be fixed by the latest updates to Net and WPF.

Post in the WPF forum for the latest info on fixing this issue.

September 1st, 2015 8:04am

Jrv  - jep sorry - overlooked the testpic thing totally. Was just focusing on the null object.

Thanks for the inform

Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 8:12am

This is what I posted:

$form.Controls|%{$_.Dispose();release-variable $_.Name

This was my amendment:
$form.Controls | %{$_.Dispose();remove-variable $_.Name}

No mention of any "testpic" was ever made as it makes no sense and cannot ever work.

Of course this is based on true code and not some shortened version of the code.

September 1st, 2015 8:22am

For the variable

Image Name="testpic" 
I have tried the following commands one at a time:

$form.Controls|%{$_.Dispose();remove-variable $_.testpic}
$form.Controls|%{$testpic.Dispose();remove-variable $_.testpic}
$form.Controls|%{$testpic.Dispose();remove-variable $testpic.name}
$form.Controls|%{$testpic.Dispose();remove-variable $testpic}
It comes up with this error:

You cannot call a method on a null-valued expression.
At line:1 char:18
+ $form.Controls|%{$testpic.Dispose();remove-variable $testpic.Name}
+                  ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Remove-Variable : Cannot bind argument to parameter 'Name' because it is null.
At line:1 char:54
+ $form.Controls|%{$testpic.Dispose();remove-variable $testpic.Name}
+                                                      ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Variable], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveVariableCommand


Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 10:37am

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

Other recent topics Other recent topics