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:54am

You need to release the form object and all controls.
Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 12:25pm

Does adding $form.close() to $btnclose here do that?

$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader etc."; exit}

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

    
    $btnclose.Add_Click({$form.Close()})
    
    
 $Form.ShowDialog() | out-null


    }

August 31st, 2015 12:52pm

You have to dispose of all of the image controls since you have created a reference.

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

This must be done after the form closes after ShowDialog returns.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 2:34pm

I tried inserting that command here:

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

    
    $btnNEXT.Add_Click({
    $form.Close()
    
    
    NEXTGUIFUNCTION

        })
    
    
 $Form.ShowDialog() | out-null

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

I get the following error:

You cannot call a method on a null-valued expression.
At D:Script.ps1:150 char:19
+  $form.Controls|%{$_.Dispose();release-variable $_.Name}
+                   ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
release-variable : The term 'release-variable' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At D:\Script.ps1:150 char:32
+  $form.Controls|%{$_.Dispose();release-variable $_.Name}
+                                ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (release-variable:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

September 1st, 2015 2:07am

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

Other recent topics Other recent topics