3

I'm trying to create a script that will output a list of Fully Qualified Names for DLLs. The script I currently have works fine when it comes to outputting the data I want.

The problem is that this locks the file. So if I want to delete one of the files that I loaded in my session I get the following message:

The action can't be completed because the file is open in Windows PowerShell.

What do I change to have PowerShell release the lock on the file?

I usually run PowerShell script from the internal console in Visual Studio Code, but the problem also exists when I run it directly from PowerShell.

Enter-PSSession -ComputerName $env:computername
Get-ChildItem -Path $input -Filter '*.dll' | ForEach-Object {
    $assembly = [System.Reflection.Assembly]::LoadFrom($_.FullName)
    Write-Host $assembly.FullName
}
Exit-PSSession
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Chrono
  • 1,433
  • 1
  • 16
  • 33

1 Answers1

3

You can use in-memory assembly:

Get-ChildItem -Path $input -Filter '*.dll' | ForEach-Object {
    $assembly = [System.Reflection.Assembly]::Load([IO.File]::ReadAllBytes($_.FullName))
    Write-Host $assembly.FullName
}
Paweł Dyl
  • 8,888
  • 1
  • 11
  • 27
  • Be sure to not load the same assembly twice, otherwise some weird situations can occur. It is a very good idea to check `AppDomain.CurrentDomain.GetAssemblies()` first before performing a load. – ogggre Mar 15 '19 at 18:11